Java Code Examples for android.app.usage.UsageStatsManager#getAppStandbyBucket()

The following examples show how to use android.app.usage.UsageStatsManager#getAppStandbyBucket() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BucketInfo.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public static @NonNull BucketInfo getInfo(@NonNull UsageStatsManager usageStatsManager, long overLastDurationMs) {
  StringBuilder stringBuilder = new StringBuilder();

  int currentBucket = usageStatsManager.getAppStandbyBucket();
  int worseBucket   = currentBucket;
  int bestBucket    = currentBucket;

  long              now           = System.currentTimeMillis();
  UsageEvents.Event event         = new UsageEvents.Event();
  UsageEvents       usageEvents   = usageStatsManager.queryEventsForSelf(now - overLastDurationMs, now);

  while (usageEvents.hasNextEvent()) {
    usageEvents.getNextEvent(event);

    if (event.getEventType() == UsageEvents.Event.STANDBY_BUCKET_CHANGED) {
      int appStandbyBucket = event.getAppStandbyBucket();

      stringBuilder.append(new Date(event.getTimeStamp()))
                   .append(": ")
                   .append("Bucket Change: ")
                   .append(bucketToString(appStandbyBucket))
                   .append("\n");

      if (appStandbyBucket > worseBucket) {
        worseBucket = appStandbyBucket;
      }
      if (appStandbyBucket < bestBucket) {
        bestBucket = appStandbyBucket;
      }
    }
  }

  return new BucketInfo(currentBucket, worseBucket, bestBucket, stringBuilder);
}
 
Example 2
Source File: Log.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
private static StringBuilder getAppInfo(Context context) {
    StringBuilder sb = new StringBuilder();

    // Get version info
    String installer = context.getPackageManager().getInstallerPackageName(BuildConfig.APPLICATION_ID);
    sb.append(String.format("%s: %s/%s %s/%s%s%s%s\r\n",
            context.getString(R.string.app_name),
            BuildConfig.APPLICATION_ID,
            installer,
            BuildConfig.VERSION_NAME,
            Helper.hasValidFingerprint(context) ? "1" : "3",
            BuildConfig.PLAY_STORE_RELEASE ? "p" : "",
            BuildConfig.DEBUG ? "d" : "",
            ActivityBilling.isPro(context) ? "+" : ""));
    sb.append(String.format("Android: %s (SDK %d)\r\n", Build.VERSION.RELEASE, Build.VERSION.SDK_INT));
    sb.append("\r\n");

    // Get device info
    sb.append(String.format("uid: %s\r\n", android.os.Process.myUid()));
    sb.append(String.format("Brand: %s\r\n", Build.BRAND));
    sb.append(String.format("Manufacturer: %s\r\n", Build.MANUFACTURER));
    sb.append(String.format("Model: %s\r\n", Build.MODEL));
    sb.append(String.format("Product: %s\r\n", Build.PRODUCT));
    sb.append(String.format("Device: %s\r\n", Build.DEVICE));
    sb.append(String.format("Host: %s\r\n", Build.HOST));
    sb.append(String.format("Display: %s\r\n", Build.DISPLAY));
    sb.append(String.format("Id: %s\r\n", Build.ID));
    sb.append("\r\n");

    sb.append(String.format("Processors: %d\r\n", Runtime.getRuntime().availableProcessors()));

    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    sb.append(String.format("Memory class: %d\r\n", am.getMemoryClass()));

    sb.append(String.format("Storage space: %s/%s\r\n",
            Helper.humanReadableByteCount(Helper.getAvailableStorageSpace(), true),
            Helper.humanReadableByteCount(Helper.getTotalStorageSpace(), true)));

    Runtime rt = Runtime.getRuntime();
    long hused = (rt.totalMemory() - rt.freeMemory()) / 1024L;
    long hmax = rt.maxMemory() / 1024L;
    long nheap = Debug.getNativeHeapAllocatedSize() / 1024L;
    sb.append(String.format("Heap usage: %s/%s KiB native: %s KiB\r\n", hused, hmax, nheap));

    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    float density = context.getResources().getDisplayMetrics().density;
    sb.append(String.format("Density %f resolution: %.2f x %.2f dp %b\r\n",
            density,
            size.x / density, size.y / density,
            context.getResources().getConfiguration().isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_NORMAL)));

    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    boolean ignoring = true;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        ignoring = pm.isIgnoringBatteryOptimizations(BuildConfig.APPLICATION_ID);
    sb.append(String.format("Battery optimizations: %b\r\n", !ignoring));

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        UsageStatsManager usm = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
        int bucket = usm.getAppStandbyBucket();
        sb.append(String.format("Standby bucket: %d\r\n", bucket));
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        boolean saving = (cm.getRestrictBackgroundStatus() == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED);
        sb.append(String.format("Data saving: %b\r\n", saving));
    }

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    boolean reporting = prefs.getBoolean("crash_reports", false);
    if (reporting) {
        String uuid = prefs.getString("uuid", null);
        sb.append(String.format("UUID: %s\r\n", uuid == null ? "-" : uuid));
    }

    sb.append("\r\n");

    sb.append(new Date(Helper.getInstallTime(context))).append("\r\n");
    sb.append(new Date()).append("\r\n");

    sb.append("\r\n");

    return sb;
}
 
Example 3
Source File: ApiTwentyEightPlus.java    From linphone-android with GNU General Public License v3.0 4 votes vote down vote up
public static int getAppStandbyBucket(Context context) {
    UsageStatsManager usageStatsManager =
            (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
    return usageStatsManager.getAppStandbyBucket();
}