Ejemplo n.º 1
0
  @Override
  public void disable(Context context) {
    SharedPreferences prefs = Probe.getPreferences(context);

    Editor e = prefs.edit();
    e.putBoolean(GitHubProbe.ENABLED, false);
    e.commit();
  }
Ejemplo n.º 2
0
  public static void loadProbeClasses(Context context) {
    String packageName = Probe.class.getPackage().getName();

    String[] probeClasses = context.getResources().getStringArray(R.array.probe_classes);

    for (String className : probeClasses) {
      try {
        Probe.registerProbeClass(Class.forName(packageName + "." + className));
      } catch (ClassNotFoundException e) {
        LogManager.getInstance(context).logException(e);
      }
    }
  }
Ejemplo n.º 3
0
  public boolean isEnabled(Context context) {
    long now = System.currentTimeMillis();

    if (now - Probe._lastEnabledCheck > 10000) {
      Probe._lastEnabledCheck = now;

      SharedPreferences prefs = Probe.getPreferences(context);

      Probe._lastEnabled = prefs.getBoolean("config_probes_enabled", false);
    }

    return Probe._lastEnabled;
  }
Ejemplo n.º 4
0
  @Override
  public PreferenceScreen preferenceScreen(final Context context, PreferenceManager manager) {
    final PreferenceScreen screen = super.preferenceScreen(context, manager);

    screen.setTitle(this.title(context));
    screen.setSummary(this.summary(context));

    CheckBoxPreference enabled = new CheckBoxPreference(context);
    enabled.setTitle(R.string.title_enable_probe);
    enabled.setKey(GitHubProbe.ENABLED);
    enabled.setDefaultValue(GitHubProbe.DEFAULT_ENABLED);

    screen.addPreference(enabled);

    final SharedPreferences prefs = Probe.getPreferences(context);

    String token = prefs.getString(GitHubProbe.OAUTH_TOKEN, null);
    String secret = prefs.getString(GitHubProbe.OAUTH_SECRET, null);

    final Preference authPreference = new Preference(context);
    authPreference.setTitle(R.string.title_authenticate_github_probe);
    authPreference.setSummary(R.string.summary_authenticate_github_probe);

    final Preference logoutPreference = new Preference(context);
    logoutPreference.setTitle(R.string.title_logout_github_probe);
    logoutPreference.setSummary(R.string.summary_logout_github_probe);

    final GitHubProbe me = this;

    authPreference.setOnPreferenceClickListener(
        new OnPreferenceClickListener() {
          @Override
          public boolean onPreferenceClick(Preference preference) {
            me.fetchAuth(context);

            screen.addPreference(logoutPreference);
            screen.removePreference(authPreference);

            return true;
          }
        });

    logoutPreference.setOnPreferenceClickListener(
        new OnPreferenceClickListener() {
          @Override
          public boolean onPreferenceClick(Preference preference) {
            Editor e = prefs.edit();
            e.remove(GitHubProbe.OAUTH_TOKEN);
            e.remove(GitHubProbe.OAUTH_SECRET);
            e.commit();

            me._lastUpdate = 0;

            screen.addPreference(authPreference);
            screen.removePreference(logoutPreference);

            if (context instanceof Activity) {
              Activity activity = (Activity) context;
              activity.runOnUiThread(
                  new Runnable() {
                    @Override
                    public void run() {
                      Toast.makeText(
                              context,
                              context.getString(R.string.toast_github_logout),
                              Toast.LENGTH_LONG)
                          .show();
                    }
                  });
            }

            return true;
          }
        });

    if (token == null || secret == null) screen.addPreference(authPreference);
    else screen.addPreference(logoutPreference);

    return screen;
  }
Ejemplo n.º 5
0
  @Override
  public boolean isEnabled(final Context context) {
    final SharedPreferences prefs = Probe.getPreferences(context);

    if (super.isEnabled(context)) {
      if (prefs.getBoolean(GitHubProbe.ENABLED, false)) {
        this.initKeystore(context);

        String token = prefs.getString(GitHubProbe.OAUTH_TOKEN, "");

        final String title = context.getString(R.string.title_github_check);
        final SanityManager sanity = SanityManager.getInstance(context);

        final GitHubProbe me = this;
        final long now = System.currentTimeMillis();

        if (token == null || token.trim().length() == 0) {
          String message = context.getString(R.string.message_github_check);

          Runnable action =
              new Runnable() {
                @Override
                public void run() {
                  me.fetchAuth(context);
                }
              };

          sanity.addAlert(SanityCheck.WARNING, title, message, action);
        } else {
          Keystore.put(GitHubApi.USER_TOKEN, token);

          sanity.clearAlert(title);

          if (now - this._lastUpdate > 1000 * 60 * 15) // 15 min refresh interval
          {
            this._lastUpdate = now;

            Runnable r =
                new Runnable() {
                  public void run() {
                    try {
                      Bundle bundle = new Bundle();
                      bundle.putString(Probe.BUNDLE_PROBE, me.name(context));

                      long lastCommit = 0;
                      String lastRepo = null;
                      String lastCommitMessage = null;

                      HashSet<String> repositories = new HashSet<>();

                      JSONObject user = GitHubApi.fetch(Uri.parse("https://api.github.com/user"));

                      bundle.putString(GitHubProbe.LOGIN, user.getString("login"));

                      JSONArray allEvents = new JSONArray();

                      for (int i = 1; i <= 10; i++) {
                        JSONArray events =
                            GitHubApi.fetchAll(
                                Uri.parse(
                                    "https://api.github.com/users/"
                                        + user.getString("login")
                                        + "/events?page="
                                        + i));

                        for (int j = 0; j < events.length(); j++) {
                          JSONObject event = events.getJSONObject(j);

                          allEvents.put(event);
                        }
                      }

                      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
                      sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

                      long now = System.currentTimeMillis();

                      HashMap<String, Integer> todayCommits = new HashMap<>();
                      HashMap<String, Integer> weekCommits = new HashMap<>();
                      HashMap<String, Integer> monthCommits = new HashMap<>();
                      HashMap<String, Integer> totalCommits = new HashMap<>();

                      for (int i = 0; i < allEvents.length(); i++) {
                        JSONObject event = allEvents.getJSONObject(i);
                        String repository = event.getJSONObject("repo").getString("name");

                        JSONObject payload = event.getJSONObject("payload");

                        if (payload.has("commits")) {
                          int commitCount = payload.getJSONArray("commits").length();

                          Date created = sdf.parse(event.getString("created_at"));

                          long timestamp = created.getTime();

                          if (timestamp > lastCommit) {
                            lastCommit = timestamp;
                            lastRepo = repository;
                            lastCommitMessage =
                                payload
                                    .getJSONArray("commits")
                                    .getJSONObject(0)
                                    .getString("message");
                          }

                          Integer today = todayCommits.get(repository);

                          if (today == null) today = 0;

                          Integer week = weekCommits.get(repository);

                          if (week == null) week = 0;

                          Integer month = monthCommits.get(repository);

                          if (month == null) month = 0;

                          Integer total = totalCommits.get(repository);

                          if (total == null) total = 0;

                          if (now - timestamp < (1000 * 60 * 60 * 24))
                            todayCommits.put(repository, today + commitCount);

                          if (now - timestamp < (1000 * 60 * 60 * 24 * 7))
                            weekCommits.put(repository, week + commitCount);

                          if (now - timestamp < (1000L * 60L * 60L * 24L * 30L))
                            monthCommits.put(repository, month + commitCount);

                          totalCommits.put(repository, total + commitCount);

                          repositories.add(repository);
                        }
                      }

                      Bundle repositoriesBundle = new Bundle();

                      int todayTotal = 0;
                      int weekTotal = 0;
                      int monthTotal = 0;
                      int total = 0;

                      for (String repository : repositories) {
                        Bundle repoBundle = new Bundle();
                        repoBundle.putString(GitHubProbe.REPOSITORY_NAME, repository);

                        if (todayCommits.containsKey(repository)) {
                          int commits = todayCommits.get(repository);
                          repoBundle.putInt(GitHubProbe.COMMITS_TODAY, commits);
                          todayTotal += commits;
                        } else repoBundle.putInt(GitHubProbe.COMMITS_TODAY, 0);

                        if (weekCommits.containsKey(repository)) {
                          int commits = weekCommits.get(repository);
                          repoBundle.putInt(GitHubProbe.COMMITS_THIS_WEEK, commits);
                          weekTotal += commits;
                        } else repoBundle.putInt(GitHubProbe.COMMITS_THIS_WEEK, 0);

                        if (monthCommits.containsKey(repository)) {
                          int commits = monthCommits.get(repository);
                          repoBundle.putInt(GitHubProbe.COMMITS_THIS_MONTH, commits);
                          monthTotal += commits;
                        } else repoBundle.putInt(GitHubProbe.COMMITS_THIS_MONTH, 0);

                        if (totalCommits.containsKey(repository)) {
                          int commits = totalCommits.get(repository);
                          repoBundle.putInt(GitHubProbe.COMMITS_TOTAL, commits);
                          total += commits;
                        } else repoBundle.putInt(GitHubProbe.COMMITS_TOTAL, 0);

                        repositoriesBundle.putBundle(repository, repoBundle);
                      }

                      bundle.putBundle(GitHubProbe.REPOSITORIES, repositoriesBundle);
                      bundle.putInt(GitHubProbe.REPOSITORY_COUNT, repositories.size());

                      bundle.putInt(GitHubProbe.COMMITS_TODAY, todayTotal);
                      bundle.putInt(GitHubProbe.COMMITS_THIS_WEEK, weekTotal);
                      bundle.putInt(GitHubProbe.COMMITS_THIS_MONTH, monthTotal);
                      bundle.putInt(GitHubProbe.COMMITS_TOTAL, total);

                      if (lastRepo != null && lastCommitMessage != null) {
                        bundle.putString(GitHubProbe.LAST_REPOSITORY, lastRepo);
                        bundle.putString(GitHubProbe.LAST_COMMIT_MESSAGE, lastCommitMessage);
                      }

                      if (lastCommit != 0) {
                        bundle.putLong(Probe.BUNDLE_TIMESTAMP, lastCommit / 1000);

                        if (lastCommit > me._lastCommit) {
                          me._lastCommit = lastCommit;
                          me.transmitData(context, bundle);
                        }
                      }
                    } catch (Exception e) {
                      LogManager.getInstance(context).logException(e);
                    }
                  }
                };

            Thread t = new Thread(r);
            t.start();
          }
        }

        return true;
      }
    }

    return false;
  }