private void addURLToTabQueue(final Intent intent, final String filename) { if (intent == null) { // This should never happen, but let's return silently instead of crashing if it does. Log.w(LOGTAG, "Error adding URL to tab queue - invalid intent passed in."); return; } final ContextUtils.SafeIntent safeIntent = new ContextUtils.SafeIntent(intent); final String intentData = safeIntent.getDataString(); // As we're doing disk IO, let's run this stuff in a separate thread. executorService.submit( new Runnable() { @Override public void run() { Context applicationContext = getApplicationContext(); final GeckoProfile profile = GeckoProfile.get(applicationContext); int tabsQueued = TabQueueHelper.queueURL(profile, intentData, filename); TabQueueHelper.showNotification(applicationContext, tabsQueued); // Store the number of URLs queued so that we don't have to read and process the file to // see if we have // any urls to open. // TODO: Use profile shared prefs when bug 1147925 gets fixed. final SharedPreferences prefs = GeckoSharedPrefs.forApp(applicationContext); prefs.edit().putInt(TabQueueHelper.PREF_TAB_QUEUE_COUNT, tabsQueued).apply(); } }); }
@Override public int onStartCommand(final Intent intent, final int flags, final int startId) { // If this is a redelivery then lets bypass the entire double tap to open now code as that's a // big can of worms, // we also don't expect redeliveries because of the short time window associated with this // feature. if (flags != START_FLAG_REDELIVERY) { final Context applicationContext = getApplicationContext(); final SharedPreferences sharedPreferences = GeckoSharedPrefs.forApp(applicationContext); final String lastUrl = sharedPreferences.getString(GeckoPreferences.PREFS_TAB_QUEUE_LAST_SITE, ""); final ContextUtils.SafeIntent safeIntent = new ContextUtils.SafeIntent(intent); final String intentUrl = safeIntent.getDataString(); final long lastRunTime = sharedPreferences.getLong(GeckoPreferences.PREFS_TAB_QUEUE_LAST_TIME, 0); final boolean isWithinDoubleTapTimeLimit = System.currentTimeMillis() - lastRunTime < TOAST_DOUBLE_TAP_TIMEOUT_MILLIS; if (!TextUtils.isEmpty(lastUrl) && lastUrl.equals(intentUrl) && isWithinDoubleTapTimeLimit) { // Background thread because we could do some file IO if we have to remove a url from the // list. tabQueueHandler.post( new Runnable() { @Override public void run() { // If there is a runnable around, that means that the previous process hasn't yet // completed, so // we will need to prevent it from running and remove the view from the window // manager. // If there is no runnable around then the url has already been added to the list, // so we'll // need to remove it before proceeding or that url will open multiple times. if (stopServiceRunnable != null) { tabQueueHandler.removeCallbacks(stopServiceRunnable); stopSelfResult(stopServiceRunnable.getStartId()); stopServiceRunnable = null; removeView(); } else { TabQueueHelper.removeURLFromFile( applicationContext, intentUrl, TabQueueHelper.FILE_NAME); } openNow(safeIntent.getUnsafe()); stopSelfResult(startId); } }); return START_REDELIVER_INTENT; } sharedPreferences .edit() .putString(GeckoPreferences.PREFS_TAB_QUEUE_LAST_SITE, intentUrl) .putLong(GeckoPreferences.PREFS_TAB_QUEUE_LAST_TIME, System.currentTimeMillis()) .apply(); } if (stopServiceRunnable != null) { // If we're already displaying a toast, keep displaying it but store the previous url. // The open button will refer to the most recently opened link. tabQueueHandler.removeCallbacks(stopServiceRunnable); stopServiceRunnable.run(false); } else { try { windowManager.addView(toastLayout, toastLayoutParams); } catch (final SecurityException e) { Toast.makeText(this, getText(R.string.tab_queue_toast_message), Toast.LENGTH_SHORT).show(); } } stopServiceRunnable = new StopServiceRunnable(startId) { @Override public void onRun() { addURLToTabQueue(intent, TabQueueHelper.FILE_NAME); stopServiceRunnable = null; } }; openNowButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(final View view) { tabQueueHandler.removeCallbacks(stopServiceRunnable); stopServiceRunnable = null; removeView(); openNow(intent); stopSelfResult(startId); } }); tabQueueHandler.postDelayed(stopServiceRunnable, TOAST_TIMEOUT); return START_REDELIVER_INTENT; }