@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(); }
@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(); }
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 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 void setCanGoBack() { ActivityDelegate a = getActivityDelegate(); if (a == null) { Log.d("Activity is null"); return; } setCanGoBack(a.getActionBar()); }
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; } }
@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(); }
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(); }
@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); }
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); }
@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); }
@Override public void onStart() { Log.d(); super.onStart(); }
@Override public void onResume() { Log.d(); super.onResume(); refreshUI(); }
@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.d(); super.onCreateView(inflater, container, savedInstanceState); setHasOptionsMenu(true); ViewGroup v = (ViewGroup) inflater.inflate(R.layout.browser, null); urlBar = (EditText) v.findViewById(R.id.urlbar); mProgressBar = (ProgressBar) v.findViewById(R.id.progressbar); // Add webview as 3rd child // TODO: I think I did this so it wouldn't be destroyed or state // lost, don't really remember. RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); layoutParams.addRule(RelativeLayout.BELOW, R.id.progressbar); layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT); layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); v.addView(mVizWebView, 2, layoutParams); /* if (initialized == false) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); String defaultUrl = prefs.getString("browser_homepage", "http://vimeo.com"); Log.d("Got default url: " + defaultUrl); urlBar.setText(defaultUrl); loadUrlFromUrlBar(); initialized = true; } */ urlBar.setOnEditorActionListener( new TextView.OnEditorActionListener() { public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if ((event != null && event.getAction() == KeyEvent.ACTION_DOWN) || actionId == EditorInfo.IME_ACTION_NEXT || actionId == EditorInfo.IME_ACTION_DONE) { Log.d("onEditorAction(actionId=" + actionId + ", event=" + event + ")"); Activity activity = getActivity(); if (activity == null) { return false; } mBrowser.loadUrlFromUrlBar(); InputMethodManager inputManager = (InputMethodManager) VizApp.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow( activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); return true; } return false; } }); setCanGoBack(); // mVizWebView.requestFocus(); // android issue #7189 (webview text fields not causing virtual // keyboard to popup) mVizWebView.requestFocus(View.FOCUS_DOWN); mVizWebView.setOnTouchListener( new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_UP: if (!v.hasFocus()) { v.requestFocus(); } break; } return false; } }); return v; }