public void confirmDownload(ResourceBuilder builder) { Context context = getActivity(); if (context == null) { Log.w("Can not confirm download without context"); return; } // Block the save dialog from popping up over an existing popup. This // is a hack put in place for the DailyMotion builder that triggers // multiple downloads for some reason. if (mConfirmationInProgress) { Log.w("Ignoring download request from builder!!!"); return; } mConfirmationInProgress = true; if (builder.isContainerURL()) { // if this is mysterious, it's no surprise -- it sucks. The link // the user clicked on was not a direct link to the content, so we // need to parse the page to get the URL. Unfortunately, we don't // have access to the downloaded content, so we have to // re-download the page and parse it. This is embarrasing and // should be fixed as it would make the user experience better, // but not sure how to do it and there are other, more interesting // goals. Log.d("Found container URL."); new ResourceParserTask().run(builder); } else { sendMessage( ActivityDelegate.MSG_BROWSER, ActivityDelegate.MSG_BROWSER_SAVEDIALOG_SHOW, builder); } }
@Override public void onCreate(Bundle savedInstanceState) { Log.d(); super.onCreate(savedInstanceState); setHasOptionsMenu(true); mBrowser = this; mVizWebViewClient = new VizWebViewClient(this); mWebChromeClient = new VizWebChromeClient(this); mVizWebView = new WebView(getActivity()); // get attributes? mVizWebView.setWebViewClient(mVizWebViewClient); mVizWebView.setWebChromeClient(mWebChromeClient); if (savedInstanceState != null) { Log.d("restoring web view state"); mVizWebView.restoreState(savedInstanceState); } WebSettings s = mVizWebView.getSettings(); s.setJavaScriptEnabled(true); s.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS); s.setBuiltInZoomControls(true); s.setUseWideViewPort(true); s.setLoadWithOverviewMode(true); s.setSaveFormData(true); mVizWebView.setId(61377); // PhoneWindow complained about no id (focus couldn't be saved) // Loading homepage here results in an exception on ICS, not sure why. // Update: post-poning until onCreatingView doesn't entirely fix the // issue either. // mVizWebView.loadUrl(defaultURL); }
private void createThumbnail(ContentValues map) throws IOException { File videoFile = fileFromResourceMap(map); Log.d("Creating thumbnail from video file " + videoFile.toString()); Bitmap bitmap = ThumbnailUtils.createVideoThumbnail( videoFile.toString(), MediaStore.Video.Thumbnails.MINI_KIND); if (bitmap == null) { Log.w("Error creating thumbnail"); return; } String filename = (String) map.get(Resources.FILENAME); if (TextUtils.isEmpty(filename)) { throw new IOException("Must specify FILENAME when inserting Resource"); } Uri thumbnailUri = Resources.buildThumbnailUri(filename + THUMBNAIL_EXT); OutputStream ostream; try { ostream = getContext().getContentResolver().openOutputStream(thumbnailUri); } catch (FileNotFoundException e) { Log.d("Could not open output stream for thumbnail storage: " + e.getLocalizedMessage()); return; } bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream); ostream.flush(); IOUtilities.closeStream(ostream); map.put(Resources.THUMBNAIL, thumbnailUri.toString()); }
private void refreshUI() { Log.d(); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); String url = prefs.getString(Preferences.LASTPAGE_LOADED, "http://vimeo.com"); Log.d("Got url from preferences: " + url); String currentURL = mVizWebView.getUrl(); if (TextUtils.isEmpty(currentURL) || !url.equals(currentURL)) { urlBar.setText(url); loadUrlFromUrlBar(); } }
private void loadUrlFromUrlBar() { String url = normalizeUrl(urlBar.getText().toString()); Log.d("(url=" + url + ")"); urlBar.setText(url); mVizWebView.loadUrl(url); setCanGoBack(); }
/** * If the download failed to complete then delete the content associated with it, as it cannot * otherwise be deleted (there's no resource record for it). */ private void deleteDownloadSideEffect(Uri downloadUri) { Log.v("deleteDownloadSideEffect(uri=" + downloadUri + ")"); Cursor cursor = query( downloadUri, new String[] {Downloads._ID, Downloads.DIRECTORY, Downloads.FILENAME, Downloads.STATUS}, null, null, null); if (!cursor.moveToFirst()) { // This is also called when deleting a Resource and there the // download could have been manually removed by the user, so this is // to be expected common. cursor.close(); return; } int statusInt = cursor.getInt(cursor.getColumnIndex(Downloads.STATUS)); Downloads.Status status = Downloads.Status.fromInt(statusInt); if (status == Downloads.Status.FAILED || status == Downloads.Status.CANCELLED || status == Downloads.Status.PAUSED) { String filename = cursor.getString(cursor.getColumnIndex(Downloads.FILENAME)); String dir = cursor.getString(cursor.getColumnIndex(Downloads.DIRECTORY)); deleteVideo(directorySwitch(dir), filename); } cursor.close(); }
/** {@inheritDoc} */ @Override public int delete(Uri uri, String selection, String[] selectionArgs) { Log.v("delete(uri=" + uri + ")"); final int match = getUriMatcher().match(uri); switch (match) { case RESOURCES_ID: deleteResourceSideEffect(uri); break; case DOWNLOADS_ID: deleteDownloadSideEffect(uri); break; default: break; } final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); final SelectionBuilder builder = buildSimpleSelection(uri); int retVal; synchronized (mutex) { retVal = builder.where(selection, selectionArgs).delete(db); } getContext().getContentResolver().notifyChange(uri, null); return retVal; }
@Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mVizWebView.saveState(outState); outState.putString("bla", "Value1"); Log.d(); }
@Override public void onDestroyView() { Log.d(); ViewGroup v = (ViewGroup) getActivity().findViewById(R.id.browserRelativeLayout); v.removeView(mVizWebView); super.onDestroyView(); }
@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { Log.d(); super.onCreateOptionsMenu(menu, inflater); inflater.inflate(R.menu.browser_menu, menu); // setCanGoBack(); }
@Override public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException { List<String> pseg = uri.getPathSegments(); if (pseg.size() < 2) { throw new FileNotFoundException("invalid uri error " + uri); } File path = getFileFromUri(uri); Log.v("openFile(uri=" + uri + ", file=" + path + ")"); int imode = 0; if (mode.contains("w")) { imode |= ParcelFileDescriptor.MODE_WRITE_ONLY; if (!path.exists()) { try { path.createNewFile(); } catch (IOException e) { e.printStackTrace(); throw new FileNotFoundException("Error creating " + uri); } } else { throw new FileNotFoundException("File with name " + path + " already exists"); } } else if (mode.contains("r")) { if (!path.exists()) { throw new FileNotFoundException("File not found " + uri); } } if (mode.contains("r")) imode |= ParcelFileDescriptor.MODE_READ_ONLY; if (mode.contains("+")) imode |= ParcelFileDescriptor.MODE_APPEND; return ParcelFileDescriptor.open(path, imode); }
@Override public void onPause() { Log.d(); String url = null; if (mVizWebView != null) { url = mVizWebView.getUrl(); } if (url != null) { Log.d("Storing url for later: " + url); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); SharedPreferences.Editor ed = prefs.edit(); ed.putString(Preferences.LASTPAGE_LOADED, url); ed.commit(); } super.onPause(); }
@Override protected void onPreExecute() { // Block user from selecting more content to download, dim screen, etc. Log.d("Sending show message"); Browser.this.sendMessage( ActivityDelegate.MSG_BROWSER, ActivityDelegate.MSG_BROWSER_TASKDIALOG_SHOW, this); }
@Override public void onActivityCreated(Bundle savedInstanceState) { Log.d(); if (savedInstanceState != null) { mVizWebView.restoreState(savedInstanceState); } super.onActivityCreated(savedInstanceState); }
private String normalizeUrl(String url) { Log.d("Normalizing url: ", url); if (url.startsWith("http://") || url.startsWith("https://")) { return url; } else { return "http://www.google.com/search?q=" + url; } }
private void setCanGoBack() { ActivityDelegate a = getActivityDelegate(); if (a == null) { Log.d("Activity is null"); return; } setCanGoBack(a.getActionBar()); }
@Override protected Void doInBackground(ResourceBuilder... builders) { Void v = null; mResourceBuilder = builders[0]; Log.d("Fetching container from " + mResourceBuilder); result = mResourceBuilder.fetchContainer(this); return v; }
public boolean goBack() { if (mVizWebView.canGoBack()) { mVizWebViewClient.goingBack(); Log.d(); mVizWebView.goBack(); return true; } return false; }
public void removeProgressDialog() { Log.d(); FragmentManager manager = getActivity().getFragmentManager(); FragmentTransaction ft = manager.beginTransaction(); Fragment prev = manager.findFragmentByTag(DIALOG_FRAGMENT_TAG); if (prev != null) { ft.remove(prev); } ft.commit(); }
private void startDownload(Resource resource) { mConfirmationInProgress = false; ActivityDelegate ad = getActivityDelegate(); if (ad == null) { return; } resource.setDownloadDirectory(VizUtils.getDownloadDir()); // check download directory in case sd card or whatever has been // unmounted and we can no longer write to it. File directory = resource.getDownloadDirectory(); if (!Utils.directoryCreate(directory)) { new AlertDialog.Builder(ad) .setIcon(R.drawable.ic_launcher) .setTitle(VizApp.getResString(R.string.download_failed)) .setMessage(VizApp.getResString(R.string.storage_error)) .setNeutralButton(R.string.ok, null) .create() .show(); return; } Uri uri = VizApp.getResolver().insert(VizContract.Downloads.CONTENT_URI, resource.toContentValues()); if (uri == null) { Log.e("Could not add download to database error"); Toast.makeText( VizApp.getContext(), VizApp.getResString(R.string.database_access_error), Toast.LENGTH_LONG) .show(); return; } Log.d("(uri=" + uri + ")"); resource.setDownloadUri(uri); Downloads downloads = ad.getDownloadsFragment(); downloads.queue(resource); }
public static ContentSource newInstance(URL url) { String host = url.getHost().toLowerCase(); Log.d("matching on: " + host); ContentSource s = sources.get(host); if (s == null || s.getResourceBuilder() == null) { s = ContentSource.GENERIC; } s.getResourceBuilder().setURL(url); return s; }
private void removeFromMediaStore(String filename) { Log.d("removeFromMediaStore(filename=" + filename + ")"); final String[] fields = { MediaStore.MediaColumns._ID, MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.TITLE }; String select = MediaStore.MediaColumns.DATA + "=?"; Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; Cursor cursor = getContext().getContentResolver().query(uri, fields, select, new String[] {filename}, null); if (cursor.moveToFirst()) { int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID)); Uri mediaUri = ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, id); getContext().getContentResolver().delete(mediaUri, null, null); getContext().getContentResolver().notifyChange(mediaUri, null); Log.d("Removing media uri: " + mediaUri); } else { Log.w("Could not find media uri"); } cursor.close(); }
/** {@inheritDoc} */ @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { Log.v("update(uri=" + uri + ")"); final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); final SelectionBuilder builder = buildSimpleSelection(uri); int rowsChanged; synchronized (mutex) { rowsChanged = builder.where(selection, selectionArgs).update(db, values); } getContext().getContentResolver().notifyChange(uri, null); return rowsChanged; }
private void deleteResourceSideEffect(Uri resourceUri) { Log.v("deleteResourceSideEffect(uri=" + resourceUri + ")"); // delete download row correspondong to this content Cursor cursor = query( resourceUri, new String[] { Resources._ID, Resources.DOWNLOAD_ID, Resources.DIRECTORY, Resources.FILENAME, Resources.CONTENT }, null, null, null); if (!cursor.moveToFirst()) { Log.e("deleteResourceSideEffect: cursor empty error"); return; } String downloadId = cursor.getString(cursor.getColumnIndex(Resources.DOWNLOAD_ID)); Uri downloadUri = Downloads.buildDownloadUri(downloadId); delete(downloadUri, null, null); getContext().getContentResolver().notifyChange(downloadUri, null); // delete video file associated with resource String filename = cursor.getString(cursor.getColumnIndex(Resources.FILENAME)); String dir = cursor.getString(cursor.getColumnIndex(Resources.DIRECTORY)); deleteVideo(directorySwitch(dir), filename); if (VizUtils.isPublicDir(dir)) { removeFromMediaStore(VizUtils.getPublicVideoFilename(filename)); } deleteThumbnail(VizContract.PATH_THUMBNAILS, filename + THUMBNAIL_EXT); cursor.close(); }
@Override public boolean onOptionsItemSelected(MenuItem item) { Log.d(); int id = item.getItemId(); if (id == android.R.id.home) { Log.d("Home pressed, go back in web history"); goBack(); return true; } else if (id == R.id.add_favorite) { Log.d("Add favorite"); Favorite favorite = Favorite.newInstance( mVizWebView.getTitle(), mVizWebView.getUrl(), mVizWebView.getFavicon()); ContentValues map = favorite.toContentValues(); getActivity().getContentResolver().insert(VizContract.Favorites.CONTENT_URI, map); getActivity().getContentResolver().notifyChange(VizContract.Favorites.CONTENT_URI, null); CharSequence text = favorite.getTitle() + " " + VizApp.getResString(R.string.favorites_added); Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show(); return true; } else { return super.onOptionsItemSelected(item); } }
/** * This is called from the webviewclient so needs to be very careful about what objects exists as * it can be called at odd times. */ public void loadUrl(String url, boolean storeFavIcon) { Log.d("(url=" + url + ")"); if (urlBar == null || mVizWebView == null) { return; } if (storeFavIcon && mWebChromeClient != null) { mWebChromeClient.storeFavIcon(url); } urlBar.setText(url); mVizWebView.loadUrl(url); setCanGoBack(); // urlBar.setText(url); }
@Override public Cursor query( Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { Log.v("query(uri=" + uri + ", proj=" + Arrays.toString(projection) + ")"); final SQLiteDatabase db = mOpenHelper.getReadableDatabase(); final int match = getUriMatcher().match(uri); final SelectionBuilder builder = buildExpandedSelection(uri, match); Cursor query; synchronized (mutex) { query = builder.where(selection, selectionArgs).query(db, projection, sortOrder); } query.setNotificationUri(getContext().getContentResolver(), uri); return query; }
@Override protected void onPostExecute(Void v) { Log.d("Sending dimiss message"); if (!result) { // if an error occurs, need to reset this so subsequent // downloads can occur mConfirmationInProgress = false; } Browser.this.sendMessage( ActivityDelegate.MSG_BROWSER, ActivityDelegate.MSG_BROWSER_TASKDIALOG_DISMISS, null); if (result) { Browser.this.sendMessage( ActivityDelegate.MSG_BROWSER, ActivityDelegate.MSG_BROWSER_SAVEDIALOG_SHOW, mResourceBuilder); } }
private File getFileFromUri(Uri uri) throws FileNotFoundException { Cursor cursor = null; String filename = null; File videoDir = null; int match = getUriMatcher().match(uri); Log.d("getFileFromUri(uri=" + uri + ", match=" + match + ")"); switch (match) { case RESOURCES_ID: cursor = query( uri, new String[] {Resources._ID, Resources.DIRECTORY, Resources.FILENAME}, null, null, null); if (!cursor.moveToFirst()) { cursor.close(); throw new FileNotFoundException("Could not find Resource for uri"); } filename = cursor.getString(cursor.getColumnIndex(Resources.FILENAME)); videoDir = directorySwitch(cursor.getString(cursor.getColumnIndex(Resources.DIRECTORY))); Log.d("Got filename: " + filename + " directory: " + videoDir); break; case DOWNLOADS_ID: cursor = query( uri, new String[] {Downloads._ID, Downloads.DIRECTORY, Downloads.FILENAME}, null, null, null); if (!cursor.moveToFirst()) { cursor.close(); throw new FileNotFoundException("Could not find Download for uri"); } filename = cursor.getString(cursor.getColumnIndex(Downloads.FILENAME)); videoDir = directorySwitch(cursor.getString(cursor.getColumnIndex(Downloads.DIRECTORY))); Log.d("Got filename: " + filename + " directory: " + videoDir); break; case RESOURCES_THUMBNAILS: Log.d("Got thumbnail: " + uri); filename = uri.getLastPathSegment(); File thumbnailDirectory = VizUtils.getVideosThumbnailDir(); Log.d("Full thumbnail directory path: " + VizUtils.getVideosThumbnailPath()); if (thumbnailDirectory == null || !thumbnailDirectory.exists()) { Log.e("Could not create directory error: " + VizUtils.getVideosThumbnailPath()); throw new FileNotFoundException("Media not mounted error: thumbnail could not be found"); } Log.d("Got thumbnail filename: " + filename); return new File(thumbnailDirectory, filename); default: throw new FileNotFoundException("No case for " + match); } cursor.close(); if (videoDir == null) { throw new FileNotFoundException("no video directory specified for " + match); } if (filename == null) { throw new FileNotFoundException("no filename specified for " + match); } return new File(videoDir, filename); }
private void deleteThumbnail(String dir, String filename) { Log.v("deleteThumbnail(dir=" + dir + ", filename=" + filename + ")"); File file = new File(getContext().getExternalFilesDir(dir), filename); file.delete(); }