Beispiel #1
0
  public static GeckoProfile get(Context context, String profileName, File profileDir) {
    if (context == null) {
      throw new IllegalArgumentException("context must be non-null");
    }

    // if no profile was passed in, look for the default profile listed in profiles.ini
    // if that doesn't exist, look for a profile called 'default'
    if (TextUtils.isEmpty(profileName) && profileDir == null) {
      profileName = GeckoProfile.findDefaultProfile(context);
      if (profileName == null) profileName = "default";
    }

    // actually try to look up the profile
    synchronized (sProfileCache) {
      GeckoProfile profile = sProfileCache.get(profileName);
      if (profile == null) {
        profile = new GeckoProfile(context, profileName);
        profile.setDir(profileDir);
        sProfileCache.put(profileName, profile);
      } else {
        profile.setDir(profileDir);
      }
      return profile;
    }
  }
  private static String addCustomProfileArg(String args) {
    String profileArg = "";
    String guestArg = "";
    if (GeckoAppShell.getGeckoInterface() != null) {
      final GeckoProfile profile = GeckoAppShell.getGeckoInterface().getProfile();

      if (profile.inGuestMode()) {
        try {
          profileArg = " -profile " + profile.getDir().getCanonicalPath();
        } catch (final IOException ioe) {
          Log.e(LOGTAG, "error getting guest profile path", ioe);
        }

        if (args == null || !args.contains(BrowserApp.GUEST_BROWSING_ARG)) {
          guestArg = " " + BrowserApp.GUEST_BROWSING_ARG;
        }
      } else if (!GeckoProfile.sIsUsingCustomProfile) {
        // If nothing was passed in the intent, make sure the default profile exists and
        // force Gecko to use the default profile for this activity
        profileArg = " -P " + profile.forceCreate().getName();
      }
    }

    return (args != null ? args : "") + profileArg + guestArg;
  }
Beispiel #3
0
  public static GeckoProfile get(Context context) {
    boolean isGeckoApp = false;
    try {
      isGeckoApp = context instanceof GeckoApp;
    } catch (NoClassDefFoundError ex) {
    }

    if (isGeckoApp) {
      // Check for a cached profile on this context already
      // TODO: We should not be caching profile information on the Activity context
      if (((GeckoApp) context).mProfile != null) {
        return ((GeckoApp) context).mProfile;
      }
    }

    // If the guest profile exists and is locked, return it
    GeckoProfile guest = GeckoProfile.getGuestProfile(context);
    if (guest != null && guest.locked()) {
      return guest;
    }

    if (isGeckoApp) {
      // Otherwise, get the default profile for the Activity
      return get(context, ((GeckoApp) context).getDefaultProfileName());
    }

    return get(context, "");
  }
  @RobocopTarget
  public static GeckoProfile get(Context context, String profileName, File profileDir) {
    if (context == null) {
      throw new IllegalArgumentException("context must be non-null");
    }

    // if no profile was passed in, look for the default profile listed in profiles.ini
    // if that doesn't exist, look for a profile called 'default'
    if (TextUtils.isEmpty(profileName) && profileDir == null) {
      try {
        profileName = GeckoProfile.getDefaultProfileName(context);
      } catch (NoMozillaDirectoryException e) {
        // We're unable to do anything sane here.
        throw new RuntimeException(e);
      }
    }

    // actually try to look up the profile
    synchronized (sProfileCache) {
      GeckoProfile profile = sProfileCache.get(profileName);
      if (profile == null) {
        try {
          profile = new GeckoProfile(context, profileName);
        } catch (NoMozillaDirectoryException e) {
          // We're unable to do anything sane here.
          throw new RuntimeException(e);
        }
        profile.setDir(profileDir);
        sProfileCache.put(profileName, profile);
      } else {
        profile.setDir(profileDir);
      }
      return profile;
    }
  }
 private static Intent getIntentForAction(final Context context, final String action) {
   final Intent intent = new Intent(action, /* uri */ null, context, GeckoService.class);
   final GeckoProfile profile = GeckoThread.getActiveProfile();
   if (profile != null) {
     setIntentProfile(intent, profile.getName(), profile.getDir().getAbsolutePath());
   }
   return intent;
 }
    @Override
    @SuppressWarnings("unchecked")
    public Void doInBackground(Object... args) {
      List<HistoricalRecord> historicalRecords = (List<HistoricalRecord>) args[0];
      String historyFileName = (String) args[1];

      FileOutputStream fos = null;

      try {
        // Mozilla - Update the location we save files to
        GeckoProfile profile = GeckoProfile.get(mContext);
        File file = profile.getFile(historyFileName);
        fos = new FileOutputStream(file);
      } catch (FileNotFoundException fnfe) {
        Log.e(LOG_TAG, "Error writing historical record file: " + historyFileName, fnfe);
        return null;
      }

      XmlSerializer serializer = Xml.newSerializer();

      try {
        serializer.setOutput(fos, null);
        serializer.startDocument("UTF-8", true);
        serializer.startTag(null, TAG_HISTORICAL_RECORDS);

        final int recordCount = historicalRecords.size();
        for (int i = 0; i < recordCount; i++) {
          HistoricalRecord record = historicalRecords.remove(0);
          serializer.startTag(null, TAG_HISTORICAL_RECORD);
          serializer.attribute(null, ATTRIBUTE_ACTIVITY, record.activity.flattenToString());
          serializer.attribute(null, ATTRIBUTE_TIME, String.valueOf(record.time));
          serializer.attribute(null, ATTRIBUTE_WEIGHT, String.valueOf(record.weight));
          serializer.endTag(null, TAG_HISTORICAL_RECORD);
          if (DEBUG) {
            Log.i(LOG_TAG, "Wrote " + record.toString());
          }
        }

        serializer.endTag(null, TAG_HISTORICAL_RECORDS);
        serializer.endDocument();

        if (DEBUG) {
          Log.i(LOG_TAG, "Wrote " + recordCount + " historical records.");
        }
      } catch (IllegalArgumentException | IOException | IllegalStateException e) {
        Log.e(LOG_TAG, "Error writing historical record file: " + mHistoryFileName, e);
      } finally {
        mCanReadHistoricalData = true;
        if (fos != null) {
          try {
            fos.close();
          } catch (IOException e) {
            /* ignore */
          }
        }
      }
      return null;
    }
  private int handleIntent(final Intent intent, final int startId) {
    if (DEBUG) {
      Log.d(LOGTAG, "Handling " + intent.getAction());
    }

    final String profileName = intent.getStringExtra(INTENT_PROFILE_NAME);
    final String profileDir = intent.getStringExtra(INTENT_PROFILE_DIR);

    if (profileName == null || profileDir == null) {
      throw new IllegalArgumentException("Intent must specify profile.");
    }

    if (!GeckoThread.initWithProfile(
        profileName != null ? profileName : "", new File(profileDir))) {
      Log.w(LOGTAG, "Ignoring due to profile mismatch: " + profileName + " [" + profileDir + ']');

      final GeckoProfile profile = GeckoThread.getActiveProfile();
      if (profile != null) {
        Log.w(
            LOGTAG,
            "Current profile is "
                + profile.getName()
                + " ["
                + profile.getDir().getAbsolutePath()
                + ']');
      }
      stopSelf(startId);
      return Service.START_NOT_STICKY;
    }

    GeckoThread.launch();

    switch (intent.getAction()) {
      case INTENT_ACTION_UPDATE_ADDONS:
        // Run the add-on update service. Because the service is automatically invoked
        // when loading Gecko, we don't have to do anything else here.
        break;

      case INTENT_ACTION_CREATE_SERVICES:
        final String category = intent.getStringExtra(INTENT_SERVICE_CATEGORY);

        if (category == null) {
          break;
        }
        GeckoThread.createServices(category);
        break;

      default:
        Log.w(LOGTAG, "Unknown request: " + intent);
    }

    stopSelf(startId);
    return Service.START_NOT_STICKY;
  }
Beispiel #8
0
 private static void fillIntentWithProfileInfo(final Context context, final Intent intent) {
   // There is a race here, but GeckoProfile returns the default profile
   // when Gecko is not explicitly running for a different profile.  In a
   // multi-profile world, this will need to be updated (possibly to
   // broadcast settings for all profiles).  See Bug 882182.
   GeckoProfile profile = GeckoProfile.get(context);
   if (profile != null) {
     intent
         .putExtra("profileName", profile.getName())
         .putExtra("profilePath", profile.getDir().getAbsolutePath());
   }
 }
Beispiel #9
0
  public static boolean maybeCleanupGuestProfile(final Context context) {
    final GeckoProfile profile = getGuestProfile(context);

    if (profile == null) {
      return false;
    } else if (!profile.locked()) {
      profile.mInGuestMode = false;

      // If the guest dir exists, but it's unlocked, delete it
      removeGuestProfile(context);

      return true;
    }
    return false;
  }
Beispiel #10
0
 public static GeckoProfile createGuestProfile(Context context) {
   try {
     removeGuestProfile(context);
     // We need to force the creation of a new guest profile if we want it outside of the normal
     // profile path,
     // otherwise GeckoProfile.getDir will try to be smart and build it for us in the normal
     // profiles dir.
     getGuestDir(context).mkdir();
     GeckoProfile profile = getGuestProfile(context);
     profile.lock();
     return profile;
   } catch (Exception ex) {
     Log.e(LOGTAG, "Error creating guest profile", ex);
   }
   return null;
 }
 public static GeckoProfile createGuestProfile(Context context) {
   if (context == null) {
     throw new IllegalArgumentException("context must be non-null");
   }
   try {
     File guestDir = context.getDir("guest", Context.MODE_PRIVATE);
     if (guestDir.exists()) guestDir.delete();
     guestDir.mkdir();
     GeckoProfile profile = get(context, "guest", guestDir);
     profile.mInGuestMode = true;
     return profile;
   } catch (Exception ex) {
     Log.e(LOGTAG, "Error creating guest profile", ex);
   }
   return null;
 }
  /**
   * Returns a SharedPreferences instance scoped to the current profile in the app. You can disable
   * migrations by using the DISABLE_MIGRATIONS flag.
   */
  public static SharedPreferences forProfile(Context context, EnumSet<Flags> flags) {
    String profileName = GeckoProfile.get(context).getName();
    if (profileName == null) {
      throw new IllegalStateException("Could not get current profile name");
    }

    return forProfileName(context, profileName, flags);
  }
 @Override
 public GeckoProfile getProfile() {
   // Fall back to default profile if we didn't load a specific one
   if (mProfile == null) {
     mProfile = GeckoProfile.get(mContext);
   }
   return mProfile;
 }
Beispiel #14
0
  @RobocopTarget
  public static String decrypt(Context context, String aValue) throws Exception {
    String resourcePath = context.getPackageResourcePath();
    GeckoLoader.loadNSSLibs(context, resourcePath);

    String path = GeckoProfile.get(context).getDir().toString();
    return nativeDecrypt(path, aValue);
  }
  private static void performMigration(Context context) {
    final SharedPreferences appPrefs = forApp(context, disableMigrations);

    final int currentVersion = appPrefs.getInt(PREFS_VERSION_KEY, 0);
    Log.d(LOGTAG, "Current version = " + currentVersion + ", prefs version = " + PREFS_VERSION);

    if (currentVersion == PREFS_VERSION) {
      return;
    }

    Log.d(LOGTAG, "Performing migration");

    final Editor appEditor = appPrefs.edit();

    // The migration always moves prefs to the default profile, not
    // the current one. We might have to revisit this if we ever support
    // multiple profiles.
    final String defaultProfileName;
    try {
      defaultProfileName = GeckoProfile.getDefaultProfileName(context);
    } catch (Exception e) {
      throw new IllegalStateException("Failed to get default profile name for migration");
    }

    final Editor profileEditor =
        forProfileName(context, defaultProfileName, disableMigrations).edit();
    final Editor crashEditor = forCrashReporter(context, disableMigrations).edit();

    List<String> profileKeys;
    Editor pmEditor = null;

    for (int v = currentVersion + 1; v <= PREFS_VERSION; v++) {
      Log.d(LOGTAG, "Migrating to version = " + v);

      switch (v) {
        case 1:
          profileKeys = Arrays.asList(PROFILE_MIGRATIONS_0_TO_1);
          pmEditor = migrateFromPreferenceManager(context, appEditor, profileEditor, profileKeys);
          break;
        case 2:
          profileKeys = Arrays.asList(PROFILE_MIGRATIONS_1_TO_2);
          migrateCrashReporterSettings(appPrefs, appEditor, crashEditor, profileKeys);
          break;
      }
    }

    // Update prefs version accordingly.
    appEditor.putInt(PREFS_VERSION_KEY, PREFS_VERSION);

    appEditor.apply();
    profileEditor.apply();
    crashEditor.apply();
    if (pmEditor != null) {
      pmEditor.apply();
    }

    Log.d(LOGTAG, "All keys have been migrated");
  }
  /**
   * Determines if the telemetry upload feature is enabled via profile & application level
   * configurations. This is the preferred method.
   *
   * <p>Note that this method logs debug statements when upload is disabled.
   */
  public static boolean isUploadEnabledByProfileConfig(
      final Context context, final GeckoProfile profile) {
    if (profile.inGuestMode()) {
      Log.d(LOGTAG, "Profile is in guest mode");
      return false;
    }

    return isUploadEnabledByAppConfig(context);
  }
  @WorkerThread
  private void uploadCorePing(
      @NonNull final String docId,
      final int seq,
      @NonNull final String profileName,
      @NonNull final String profilePath,
      @Nullable final String defaultSearchEngine) {
    final GeckoProfile profile = GeckoProfile.get(this, profileName, profilePath);
    final String clientId;
    try {
      clientId = profile.getClientId();
    } catch (final IOException e) {
      Log.w(LOGTAG, "Unable to get client ID to generate core ping: returning.", e);
      return;
    }

    // Each profile can have different telemetry data so we intentionally grab the shared prefs for
    // the profile.
    final SharedPreferences sharedPrefs = GeckoSharedPrefs.forProfileName(this, profileName);
    // TODO (bug 1241685): Sync this preference with the gecko preference.
    final String serverURLSchemeHostPort =
        sharedPrefs.getString(
            TelemetryConstants.PREF_SERVER_URL, TelemetryConstants.DEFAULT_SERVER_URL);

    final long profileCreationDate = getProfileCreationDate(profile);
    final TelemetryCorePingBuilder builder =
        new TelemetryCorePingBuilder(this, serverURLSchemeHostPort)
            .setClientID(clientId)
            .setDefaultSearchEngine(
                TextUtils.isEmpty(defaultSearchEngine) ? null : defaultSearchEngine)
            .setProfileCreationDate(profileCreationDate < 0 ? null : profileCreationDate)
            .setSequenceNumber(seq);

    final String distributionId =
        sharedPrefs.getString(DistributionStoreCallback.PREF_DISTRIBUTION_ID, null);
    if (distributionId != null) {
      builder.setOptDistributionID(distributionId);
    }

    final TelemetryPing corePing = builder.build();
    final CorePingResultDelegate resultDelegate = new CorePingResultDelegate();
    uploadPing(corePing, resultDelegate);
  }
 /** @return the profile creation date in the format expected by TelemetryPingGenerator. */
 @WorkerThread
 private long getProfileCreationDate(final GeckoProfile profile) {
   final long profileMillis = profile.getAndPersistProfileCreationDate(this);
   // getAndPersistProfileCreationDate can return -1,
   // and we don't want to truncate (-1 / MILLIS) to 0.
   if (profileMillis < 0) {
     return profileMillis;
   }
   return (long) Math.floor((double) profileMillis / MILLIS_IN_DAY);
 }
Beispiel #19
0
 private static File getPingFile() {
   if (GeckoAppShell.getContext() == null) {
     return null;
   }
   GeckoProfile profile = GeckoAppShell.getGeckoInterface().getProfile();
   if (profile == null) {
     return null;
   }
   File profDir = profile.getDir();
   if (profDir == null) {
     return null;
   }
   File pingDir = new File(profDir, "saved-telemetry-pings");
   pingDir.mkdirs();
   if (!(pingDir.exists() && pingDir.isDirectory())) {
     return null;
   }
   return new File(pingDir, UUID.randomUUID().toString());
 }
  public static boolean removeProfile(Context context, String profileName) {
    if (profileName == null) {
      Log.w(LOGTAG, "Unable to remove profile: null profile name.");
      return false;
    }

    final GeckoProfile profile = get(context, profileName);
    if (profile == null) {
      return false;
    }
    final boolean success = profile.remove();

    if (success) {
      // Clear all shared prefs for the given profile.
      GeckoSharedPrefs.forProfileName(context, profileName).edit().clear().commit();
    }

    return success;
  }
Beispiel #21
0
  private static GeckoProfile getGuestProfile(Context context) {
    if (mGuestProfile == null) {
      File guestDir = getGuestDir(context);
      if (guestDir.exists()) {
        mGuestProfile = get(context, "guest", guestDir);
        mGuestProfile.mInGuestMode = true;
      }
    }

    return mGuestProfile;
  }
  private static GeckoProfile getGuestProfile(Context context) {
    if (sGuestProfile == null) {
      File guestDir = getGuestDir(context);
      if (guestDir.exists()) {
        sGuestProfile = get(context, GUEST_PROFILE, guestDir);
        sGuestProfile.mInGuestMode = true;
      }
    }

    return sGuestProfile;
  }
  /** Command for reading the historical records from a file off the UI thread. */
  private void readHistoricalDataImpl() {
    try {
      GeckoProfile profile = GeckoProfile.get(mContext);
      File f = profile.getFile(mHistoryFileName);
      if (!f.exists()) {
        // Fall back to the non-profile aware file if it exists...
        File oldFile = new File(mHistoryFileName);
        oldFile.renameTo(f);
      }
      readHistoricalDataFromStream(new FileInputStream(f));
    } catch (FileNotFoundException fnfe) {
      final Distribution dist = Distribution.getInstance(mContext);
      dist.addOnDistributionReadyCallback(
          new Runnable() {
            @Override
            public void run() {
              Log.d(LOGTAG, "Running post-distribution task: quickshare.");

              if (!dist.exists()) {
                return;
              }

              try {
                File distFile = dist.getDistributionFile("quickshare/" + mHistoryFileName);
                if (distFile == null) {
                  if (DEBUG) {
                    Log.i(LOG_TAG, "Could not open historical records file: " + mHistoryFileName);
                  }
                  return;
                }
                readHistoricalDataFromStream(new FileInputStream(distFile));
              } catch (Exception ex) {
                if (DEBUG) {
                  Log.i(LOG_TAG, "Could not open historical records file: " + mHistoryFileName);
                }
                return;
              }
            }
          });
    }
  }
  public static GeckoProfile get(Context context) {
    boolean isGeckoApp = false;
    try {
      isGeckoApp = context instanceof GeckoApp;
    } catch (NoClassDefFoundError ex) {
    }

    if (isGeckoApp) {
      // Check for a cached profile on this context already
      // TODO: We should not be caching profile information on the Activity context
      final GeckoApp geckoApp = (GeckoApp) context;
      if (geckoApp.mProfile != null) {
        return geckoApp.mProfile;
      }
    }

    // If the guest profile exists and is locked, return it
    GeckoProfile guest = GeckoProfile.getGuestProfile(context);
    if (guest != null && guest.locked()) {
      return guest;
    }

    if (isGeckoApp) {
      final GeckoApp geckoApp = (GeckoApp) context;
      String defaultProfileName;
      try {
        defaultProfileName = geckoApp.getDefaultProfileName();
      } catch (NoMozillaDirectoryException e) {
        // If this failed, we're screwed. But there are so many callers that
        // we'll just throw a RuntimeException.
        Log.wtf(LOGTAG, "Unable to get default profile name.", e);
        throw new RuntimeException(e);
      }
      // Otherwise, get the default profile for the Activity.
      return get(context, defaultProfileName);
    }

    return get(context, "");
  }
  public static GeckoProfile createGuestProfile(Context context) {
    try {
      removeGuestProfile(context);
      // We need to force the creation of a new guest profile if we want it outside of the normal
      // profile path,
      // otherwise GeckoProfile.getDir will try to be smart and build it for us in the normal
      // profiles dir.
      getGuestDir(context).mkdir();
      GeckoProfile profile = getGuestProfile(context);
      profile.lock();

      /*
       * Now do the things that createProfileDirectory normally does --
       * right now that's kicking off DB init.
       */
      profile.enqueueInitialization();

      return profile;
    } catch (Exception ex) {
      Log.e(LOGTAG, "Error creating guest profile", ex);
    }
    return null;
  }
Beispiel #26
0
  private void initialize() {
    mHeader = (RelativeLayout) findViewById(R.id.tabs_panel_header);
    mTabsContainer = (TabsListContainer) findViewById(R.id.tabs_container);

    mPanelNormal = (PanelView) findViewById(R.id.normal_tabs);
    mPanelNormal.setTabsPanel(this);

    mPanelPrivate = (PanelView) findViewById(R.id.private_tabs_panel);
    mPanelPrivate.setTabsPanel(this);

    mPanelRemote = (PanelView) findViewById(R.id.remote_tabs);
    mPanelRemote.setTabsPanel(this);

    mFooter = (RelativeLayout) findViewById(R.id.tabs_panel_footer);

    mAddTab = (ImageButton) findViewById(R.id.add_tab);
    mAddTab.setOnClickListener(
        new Button.OnClickListener() {
          @Override
          public void onClick(View v) {
            TabsPanel.this.addTab();
          }
        });

    mTabWidget = (IconTabWidget) findViewById(R.id.tab_widget);

    mTabWidget.addTab(R.drawable.tabs_normal, R.string.tabs_normal);
    mTabWidget.addTab(R.drawable.tabs_private, R.string.tabs_private);

    if (!GeckoProfile.get(mContext).inGuestMode()) {
      // The initial icon is not the animated icon, because on Android
      // 4.4.2, the animation starts immediately (and can start at other
      // unpredictable times). See Bug 1015974.
      mTabWidget.addTab(R.drawable.tabs_synced, R.string.tabs_synced);
    }

    mTabWidget.setTabSelectionListener(this);

    mMenuButton = (ImageButton) findViewById(R.id.menu);
    mMenuButton.setOnClickListener(
        new Button.OnClickListener() {
          @Override
          public void onClick(View view) {
            showMenu();
          }
        });
  }
Beispiel #27
0
 public static void leaveGuestSession(Context context) {
   GeckoProfile profile = getGuestProfile(context);
   if (profile != null) {
     profile.unlock();
   }
 }
Beispiel #28
0
  /**
   * Recursively loop through a PreferenceGroup. Initialize native Android prefs, and build a list
   * of Gecko preferences in the passed in prefs array
   *
   * @param preferences The android.preference.PreferenceGroup to initialize
   * @param prefs An ArrayList to fill with Gecko preferences that need to be initialized
   * @return The integer id for the PrefsHelper.PrefHandlerBase listener added to monitor changes to
   *     Gecko prefs.
   */
  private void setupPreferences(PreferenceGroup preferences, ArrayList<String> prefs) {
    for (int i = 0; i < preferences.getPreferenceCount(); i++) {
      Preference pref = preferences.getPreference(i);
      String key = pref.getKey();
      if (pref instanceof PreferenceGroup) {
        // If no datareporting is enabled, remove UI.
        if (PREFS_DATA_REPORTING_PREFERENCES.equals(key)) {
          if (!AppConstants.MOZ_DATA_REPORTING) {
            preferences.removePreference(pref);
            i--;
            continue;
          }
        } else if (pref instanceof PanelsPreferenceCategory) {
          mPanelsPreferenceCategory = (PanelsPreferenceCategory) pref;
        }
        setupPreferences((PreferenceGroup) pref, prefs);
      } else {
        pref.setOnPreferenceChangeListener(this);
        if (!AppConstants.MOZ_UPDATER && PREFS_UPDATER_AUTODOWNLOAD.equals(key)) {
          preferences.removePreference(pref);
          i--;
          continue;
        } else if (AppConstants.RELEASE_BUILD && PREFS_DISPLAY_REFLOW_ON_ZOOM.equals(key)) {
          // Remove UI for reflow on release builds.
          preferences.removePreference(pref);
          i--;
          continue;
        } else if (!AppConstants.MOZ_TELEMETRY_REPORTING && PREFS_TELEMETRY_ENABLED.equals(key)) {
          preferences.removePreference(pref);
          i--;
          continue;
        } else if (!AppConstants.MOZ_SERVICES_HEALTHREPORT
            && (PREFS_HEALTHREPORT_UPLOAD_ENABLED.equals(key)
                || PREFS_HEALTHREPORT_LINK.equals(key))) {
          preferences.removePreference(pref);
          i--;
          continue;
        } else if (!AppConstants.MOZ_CRASHREPORTER && PREFS_CRASHREPORTER_ENABLED.equals(key)) {
          preferences.removePreference(pref);
          i--;
          continue;
        } else if (AppConstants.RELEASE_BUILD && PREFS_GEO_REPORTING.equals(key)) {
          // We don't build wifi/cell tower collection in release builds, so hide the UI.
          preferences.removePreference(pref);
          i--;
          continue;
        } else if (PREFS_DEVTOOLS_REMOTE_ENABLED.equals(key)) {
          final Context thisContext = this;
          pref.setOnPreferenceClickListener(
              new OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {
                  // Display toast to remind setting up tcp forwarding.
                  if (((CheckBoxPreference) preference).isChecked()) {
                    Toast.makeText(
                            thisContext,
                            R.string.devtools_remote_debugging_forward,
                            Toast.LENGTH_SHORT)
                        .show();
                  }
                  return true;
                }
              });
        } else if (PREFS_RESTORE_SESSION.equals(key)) {
          // Set the summary string to the current entry. The summary
          // for other list prefs will be set in the PrefsHelper
          // callback, but since this pref doesn't live in Gecko, we
          // need to handle it separately.
          ListPreference listPref = (ListPreference) pref;
          CharSequence selectedEntry = listPref.getEntry();
          listPref.setSummary(selectedEntry);
          continue;
        } else if (PREFS_SYNC.equals(key) && GeckoProfile.get(this).inGuestMode()) {
          // Don't show sync prefs while in guest mode.
          preferences.removePreference(pref);
          i--;
          continue;
        } else if (PREFS_SEARCH_RESTORE_DEFAULTS.equals(key)) {
          pref.setOnPreferenceClickListener(
              new OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {
                  GeckoPreferences.this.restoreDefaultSearchEngines();
                  return true;
                }
              });
        } else if (PREFS_HOME_ADD_PANEL.equals(key)) {
          pref.setOnPreferenceClickListener(
              new OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {
                  Intent dialogIntent = new Intent(GeckoPreferences.this, HomePanelPicker.class);
                  startActivityForResult(dialogIntent, HomePanelPicker.REQUEST_CODE_ADD_PANEL);
                  return true;
                }
              });
        }

        // Some Preference UI elements are not actually preferences,
        // but they require a key to work correctly. For example,
        // "Clear private data" requires a key for its state to be
        // saved when the orientation changes. It uses the
        // "android.not_a_preference.privacy.clear" key - which doesn't
        // exist in Gecko - to satisfy this requirement.
        if (key != null && !key.startsWith(NON_PREF_PREFIX)) {
          prefs.add(key);
        }
      }
    }
  }
Beispiel #29
0
  private void init(Context context, String url, boolean doInit) {
    // TODO: Fennec currently takes care of its own initialization, so this
    // flag is a hack used in Fennec to prevent GeckoView initialization.
    // This should go away once Fennec also uses GeckoView for
    // initialization.
    if (!doInit) return;

    // If running outside of a GeckoActivity (eg, from a library project),
    // load the native code and disable content providers
    boolean isGeckoActivity = false;
    try {
      isGeckoActivity = context instanceof GeckoActivity;
    } catch (NoClassDefFoundError ex) {
    }

    if (!isGeckoActivity) {
      // Set the GeckoInterface if the context is an activity and the GeckoInterface
      // has not already been set
      if (context instanceof Activity && getGeckoInterface() == null) {
        setGeckoInterface(new BaseGeckoInterface(context));
      }

      Clipboard.init(context);
      HardwareUtils.init(context);

      // If you want to use GeckoNetworkManager, start it.

      GeckoLoader.loadMozGlue();
      BrowserDB.setEnableContentProviders(false);
    }

    if (url != null) {
      GeckoThread.setUri(url);
      GeckoThread.setAction(Intent.ACTION_VIEW);
      GeckoAppShell.sendEventToGecko(GeckoEvent.createURILoadEvent(url));
    }
    GeckoAppShell.setContextGetter(this);
    if (context instanceof Activity) {
      Tabs tabs = Tabs.getInstance();
      tabs.attachToContext(context);
    }

    EventDispatcher.getInstance()
        .registerGeckoThreadListener(
            this,
            "Gecko:Ready",
            "Content:StateChange",
            "Content:LoadError",
            "Content:PageShow",
            "DOMTitleChanged",
            "Link:Favicon",
            "Prompt:Show",
            "Prompt:ShowTop");

    ThreadUtils.setUiThread(Thread.currentThread(), new Handler());
    initializeView(EventDispatcher.getInstance());

    if (GeckoThread.checkAndSetLaunchState(
        GeckoThread.LaunchState.Launching, GeckoThread.LaunchState.Launched)) {
      // This is the first launch, so finish initialization and go.
      GeckoProfile profile = GeckoProfile.get(context).forceCreate();
      BrowserDB.initialize(profile.getName());

      GeckoAppShell.setLayerView(this);
      GeckoThread.createAndStart();
    } else if (GeckoThread.checkLaunchState(GeckoThread.LaunchState.GeckoRunning)) {
      // If Gecko is already running, that means the Activity was
      // destroyed, so we need to re-attach Gecko to this GeckoView.
      connectToGecko();
    }
  }
  /** Command for reading the historical records from a file off the UI thread. */
  private void readHistoricalDataImpl() {
    FileInputStream fis = null;
    try {
      GeckoProfile profile = GeckoProfile.get(mContext);
      File f = profile.getFile(mHistoryFileName);
      if (!f.exists()) {
        // Fall back to the non-profile aware file if it exists...
        File oldFile = new File(mHistoryFileName);
        oldFile.renameTo(f);
      }
      fis = new FileInputStream(f);
    } catch (FileNotFoundException fnfe) {
      try {
        Distribution dist = new Distribution(mContext);
        File distFile = dist.getDistributionFile("quickshare/" + mHistoryFileName);
        if (distFile == null) {
          if (DEBUG) {
            Log.i(LOG_TAG, "Could not open historical records file: " + mHistoryFileName);
          }
          return;
        }
        fis = new FileInputStream(distFile);
      } catch (Exception ex) {
        if (DEBUG) {
          Log.i(LOG_TAG, "Could not open historical records file: " + mHistoryFileName);
        }
        return;
      }
    }

    try {
      XmlPullParser parser = Xml.newPullParser();
      parser.setInput(fis, null);

      int type = XmlPullParser.START_DOCUMENT;
      while (type != XmlPullParser.END_DOCUMENT && type != XmlPullParser.START_TAG) {
        type = parser.next();
      }

      if (!TAG_HISTORICAL_RECORDS.equals(parser.getName())) {
        throw new XmlPullParserException(
            "Share records file does not start with " + TAG_HISTORICAL_RECORDS + " tag.");
      }

      List<HistoricalRecord> historicalRecords = mHistoricalRecords;
      historicalRecords.clear();

      while (true) {
        type = parser.next();
        if (type == XmlPullParser.END_DOCUMENT) {
          break;
        }
        if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
          continue;
        }
        String nodeName = parser.getName();
        if (!TAG_HISTORICAL_RECORD.equals(nodeName)) {
          throw new XmlPullParserException("Share records file not well-formed.");
        }

        String activity = parser.getAttributeValue(null, ATTRIBUTE_ACTIVITY);
        final long time = Long.parseLong(parser.getAttributeValue(null, ATTRIBUTE_TIME));
        final float weight = Float.parseFloat(parser.getAttributeValue(null, ATTRIBUTE_WEIGHT));
        HistoricalRecord readRecord = new HistoricalRecord(activity, time, weight);
        historicalRecords.add(readRecord);

        if (DEBUG) {
          Log.i(LOG_TAG, "Read " + readRecord.toString());
        }
      }

      if (DEBUG) {
        Log.i(LOG_TAG, "Read " + historicalRecords.size() + " historical records.");
      }
    } catch (XmlPullParserException xppe) {
      Log.e(LOG_TAG, "Error reading historical recrod file: " + mHistoryFileName, xppe);
    } catch (IOException ioe) {
      Log.e(LOG_TAG, "Error reading historical recrod file: " + mHistoryFileName, ioe);
    } finally {
      if (fis != null) {
        try {
          fis.close();
        } catch (IOException ioe) {
          /* ignore */
        }
      }
    }
  }