private void openNow(Intent intent) {
    Intent forwardIntent = new Intent(intent);
    forwardIntent.setClassName(
        getApplicationContext(), AppConstants.MOZ_ANDROID_BROWSER_INTENT_CLASS);
    forwardIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(forwardIntent);

    TabQueueHelper.removeNotification(getApplicationContext());

    GeckoSharedPrefs.forApp(getApplicationContext())
        .edit()
        .remove(GeckoPreferences.PREFS_TAB_QUEUE_LAST_SITE)
        .remove(GeckoPreferences.PREFS_TAB_QUEUE_LAST_TIME)
        .apply();

    Telemetry.sendUIEvent(
        TelemetryContract.Event.LOAD_URL, TelemetryContract.Method.INTENT, "tabqueue-now");

    executorService.submit(
        new Runnable() {
          @Override
          public void run() {
            int queuedTabCount = TabQueueHelper.getTabQueueLength(TabQueueService.this);
            Telemetry.addToHistogram("FENNEC_TABQUEUE_QUEUESIZE", queuedTabCount);
          }
        });
  }
Exemple #2
0
 private SharedPreferences getSharedPreferences() {
   final SharedPreferences settings;
   if (prefsBranch == null) {
     settings = GeckoSharedPrefs.forApp(context);
   } else {
     settings = context.getSharedPreferences(prefsBranch, Activity.MODE_PRIVATE);
   }
   return settings;
 }
  public void testDisabledState() {
    resources.setSuggestedSitesResource(generateSites(3));

    Cursor c = new SuggestedSites(context).get(DEFAULT_LIMIT);
    assertEquals(3, c.getCount());
    c.close();

    // Disable suggested sites
    GeckoSharedPrefs.forApp(context)
        .edit()
        .putBoolean(GeckoPreferences.PREFS_SUGGESTED_SITES, false)
        .commit();

    c = new SuggestedSites(context).get(DEFAULT_LIMIT);
    assertNotNull(c);
    assertEquals(0, c.getCount());
    c.close();
  }
  @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;
  }