Example #1
0
  /** Testa la correttezza del metodo "compareTo". */
  @Test
  public void testCompareTo() {
    Date te1_1 = new Date(100);
    Date te1_2 = new Date(200);
    Date te1_3 = new Date(300);
    Date[] t1_events = {te1_1, te1_2, te1_3};
    Telemetry t1 = new Telemetry(t1_events, "track");

    Date te2_1 = new Date(100);
    Date te2_2 = new Date(200);
    Date te2_3 = new Date(300);
    Date[] t2_events = {te2_1, te2_2, te2_3};
    Telemetry t2 = new Telemetry(t2_events, "track");

    Date te3_1 = new Date(100);
    Date te3_2 = new Date(150);
    Date te3_3 = new Date(200);
    Date[] t3_events = {te3_1, te3_2, te3_3};
    Telemetry t3 = new Telemetry(t3_events, "track");

    assertTrue(t1.compareTo(t2) == 0);
    assertTrue(t2.compareTo(t1) == 0);
    assertTrue(t1.compareTo(t3) == 1);
    assertTrue(t3.compareTo(t1) == -1);
  }
Example #2
0
  /** Testa la correttezza del costruttore e dei metodi "getter" della classe. */
  @Test
  public void testConstructor() {
    Date first_event = new Date(1124242);
    Date second_event = new Date(95843);
    Date[] events = {first_event, second_event};
    Iterable<Date> i_events = Arrays.asList(events);
    Telemetry t1 = new Telemetry(events, "track");
    Telemetry t2 = new Telemetry(i_events, "track");
    Iterable<Date> t1_events = t1.getEvents();
    Iterable<Date> t2_events = t2.getEvents();
    Iterator<Date> t1_iterator = t1_events.iterator();
    Iterator<Date> t2_iterator = t2_events.iterator();
    Iterator<Date> input_iterator = i_events.iterator();
    while (t1_iterator.hasNext() && t2_iterator.hasNext() && input_iterator.hasNext()) {
      Date te1 = t1_iterator.next();
      Date te2 = t2_iterator.next();
      Date ie = input_iterator.next();

      assertTrue(te1.getTime() == ie.getTime());
      assertTrue(te2.getTime() == ie.getTime());
    }
  }
  private File createProfileDir() throws IOException {
    INIParser parser = GeckoProfileDirectories.getProfilesINI(mMozillaDir);

    // Salt the name of our requested profile
    String saltedName = GeckoProfileDirectories.saltProfileName(mName);
    File profileDir = new File(mMozillaDir, saltedName);
    while (profileDir.exists()) {
      saltedName = GeckoProfileDirectories.saltProfileName(mName);
      profileDir = new File(mMozillaDir, saltedName);
    }

    // Attempt to create the salted profile dir
    if (!profileDir.mkdirs()) {
      throw new IOException("Unable to create profile.");
    }
    Log.d(LOGTAG, "Created new profile dir.");

    // Now update profiles.ini
    // If this is the first time its created, we also add a General section
    // look for the first profile number that isn't taken yet
    int profileNum = 0;
    boolean isDefaultSet = false;
    INISection profileSection;
    while ((profileSection = parser.getSection("Profile" + profileNum)) != null) {
      profileNum++;
      if (profileSection.getProperty("Default") != null) {
        isDefaultSet = true;
      }
    }

    profileSection = new INISection("Profile" + profileNum);
    profileSection.setProperty("Name", mName);
    profileSection.setProperty("IsRelative", 1);
    profileSection.setProperty("Path", saltedName);

    if (parser.getSection("General") == null) {
      INISection generalSection = new INISection("General");
      generalSection.setProperty("StartWithLastProfile", 1);
      parser.addSection(generalSection);
    }

    if (!isDefaultSet && !mIsWebAppProfile) {
      // only set as default if this is the first non-webapp
      // profile we're creating
      profileSection.setProperty("Default", 1);

      // We have no intention of stopping this session. The FIRSTRUN session
      // ends when the browsing session/activity has ended. All events
      // during firstrun will be tagged as FIRSTRUN.
      Telemetry.startUISession(TelemetryContract.Session.FIRSTRUN);
    }

    parser.addSection(profileSection);
    parser.write();

    // Trigger init for non-webapp profiles.
    if (!mIsWebAppProfile) {
      enqueueInitialization();
    }

    // Write out profile creation time, mirroring the logic in nsToolkitProfileService.
    try {
      FileOutputStream stream =
          new FileOutputStream(profileDir.getAbsolutePath() + File.separator + "times.json");
      OutputStreamWriter writer = new OutputStreamWriter(stream, Charset.forName("UTF-8"));
      try {
        writer.append("{\"created\": " + System.currentTimeMillis() + "}\n");
      } finally {
        writer.close();
      }
    } catch (Exception e) {
      // Best-effort.
      Log.w(LOGTAG, "Couldn't write times.json.", e);
    }

    // Initialize pref flag for displaying the start pane for a new non-webapp profile.
    if (!mIsWebAppProfile) {
      final SharedPreferences prefs = GeckoSharedPrefs.forProfile(mApplicationContext);
      prefs.edit().putBoolean(BrowserApp.PREF_STARTPANE_ENABLED, true).apply();
    }

    return profileDir;
  }
Example #4
0
  @Override
  public void handleMessage(String event, JSONObject message) {
    try {
      if (event.equals("Menu:Add")) {
        MenuItemInfo info = new MenuItemInfo();
        info.label = message.getString("name");
        info.id = message.getInt("id") + ADDON_MENU_OFFSET;
        info.checkable = false;
        info.checked = false;
        info.enabled = true;
        info.visible = true;
        String iconRes = null;
        try { // icon is optional
          iconRes = message.getString("icon");
        } catch (Exception ex) {
        }
        info.icon = iconRes;
        info.checkable = false;
        try {
          info.checkable = message.getBoolean("checkable");
        } catch (Exception ex) {
        }
        try { // parent is optional
          info.parent = message.getInt("parent") + ADDON_MENU_OFFSET;
        } catch (Exception ex) {
        }
        final MenuItemInfo menuItemInfo = info;
        mMainHandler.post(
            new Runnable() {
              public void run() {
                addAddonMenuItem(menuItemInfo);
              }
            });
      } else if (event.equals("Menu:Remove")) {
        final int id = message.getInt("id") + ADDON_MENU_OFFSET;
        mMainHandler.post(
            new Runnable() {
              public void run() {
                removeAddonMenuItem(id);
              }
            });
      } else if (event.equals("Menu:Update")) {
        final int id = message.getInt("id") + ADDON_MENU_OFFSET;
        final JSONObject options = message.getJSONObject("options");
        mMainHandler.post(
            new Runnable() {
              public void run() {
                updateAddonMenuItem(id, options);
              }
            });
      } else if (event.equals("CharEncoding:Data")) {
        final JSONArray charsets = message.getJSONArray("charsets");
        int selected = message.getInt("selected");

        final int len = charsets.length();
        final String[] titleArray = new String[len];
        for (int i = 0; i < len; i++) {
          JSONObject charset = charsets.getJSONObject(i);
          titleArray[i] = charset.getString("title");
        }

        final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
        dialogBuilder.setSingleChoiceItems(
            titleArray,
            selected,
            new AlertDialog.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {
                try {
                  JSONObject charset = charsets.getJSONObject(which);
                  GeckoAppShell.sendEventToGecko(
                      GeckoEvent.createBroadcastEvent(
                          "CharEncoding:Set", charset.getString("code")));
                  dialog.dismiss();
                } catch (JSONException e) {
                  Log.e(LOGTAG, "error parsing json", e);
                }
              }
            });
        dialogBuilder.setNegativeButton(
            R.string.button_cancel,
            new AlertDialog.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
              }
            });
        mMainHandler.post(
            new Runnable() {
              public void run() {
                dialogBuilder.show();
              }
            });
      } else if (event.equals("CharEncoding:State")) {
        final boolean visible = message.getString("visible").equals("true");
        GeckoPreferences.setCharEncodingState(visible);
        final Menu menu = mMenu;
        mMainHandler.post(
            new Runnable() {
              public void run() {
                if (menu != null) menu.findItem(R.id.char_encoding).setVisible(visible);
              }
            });
      } else if (event.equals("Feedback:OpenPlayStore")) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("market://details?id=" + getPackageName()));
        startActivity(intent);
      } else if (event.equals("Feedback:MaybeLater")) {
        resetFeedbackLaunchCount();
      } else if (event.equals("Feedback:LastUrl")) {
        getLastUrl();
      } else if (event.equals("Gecko:Ready")) {
        // Handle this message in GeckoApp, but also enable the Settings
        // menuitem, which is specific to BrowserApp.
        super.handleMessage(event, message);
        final Menu menu = mMenu;
        mMainHandler.post(
            new Runnable() {
              public void run() {
                if (menu != null) menu.findItem(R.id.settings).setEnabled(true);
              }
            });
      } else if (event.equals("Telemetry:Gather")) {
        Telemetry.HistogramAdd(
            "PLACES_PAGES_COUNT", BrowserDB.getCount(getContentResolver(), "history"));
        Telemetry.HistogramAdd(
            "PLACES_BOOKMARKS_COUNT", BrowserDB.getCount(getContentResolver(), "bookmarks"));
        Telemetry.HistogramAdd(
            "FENNEC_FAVICONS_COUNT", BrowserDB.getCount(getContentResolver(), "favicons"));
        Telemetry.HistogramAdd(
            "FENNEC_THUMBNAILS_COUNT", BrowserDB.getCount(getContentResolver(), "thumbnails"));
      } else if (event.equals("Dex:Load")) {
        String zipFile = message.getString("zipfile");
        String implClass = message.getString("impl");
        Log.d(
            LOGTAG,
            "Attempting to load classes.dex file from "
                + zipFile
                + " and instantiate "
                + implClass);
        try {
          File tmpDir = getDir("dex", 0);
          DexClassLoader loader =
              new DexClassLoader(
                  zipFile, tmpDir.getAbsolutePath(), null, ClassLoader.getSystemClassLoader());
          Class<?> c = loader.loadClass(implClass);
          c.newInstance();
        } catch (Exception e) {
          Log.e(LOGTAG, "Unable to initialize addon", e);
        }
      } else {
        super.handleMessage(event, message);
      }
    } catch (Exception e) {
      Log.e(LOGTAG, "Exception handling message \"" + event + "\":", e);
    }
  }