Search in sources :

Example 36 with StatElement

use of com.asksven.android.common.privateapiproxies.StatElement in project BetterBatteryStats by asksven.

the class WatchdogProcessingService method onHandleIntent.

@Override
public void onHandleIntent(Intent intent) {
    Log.i(TAG, "Called at " + DateUtils.now());
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    try {
        if (true) {
            int minScreenOffDurationMin = sharedPrefs.getInt("watchdog_duration_threshold", 10);
            int awakeThresholdPct = sharedPrefs.getInt("watchdog_awake_threshold", 30);
            long now = System.currentTimeMillis();
            Long screenOffTime = sharedPrefs.getLong("screen_went_off_at", now);
            Long screenOffDurationMs = now - screenOffTime;
            // we process only if screenOffDuration is >= minScreenOffDuration
            if (screenOffDurationMs >= ((long) minScreenOffDurationMin * 60 * 1000)) {
                // Toast.makeText(this, getString(R.string.message_watchdog_processing), Toast.LENGTH_SHORT).show();
                int awakePct = 0;
                StatsProvider stats = StatsProvider.getInstance();
                // make sure to flush cache
                BatteryStatsProxy.getInstance(this).invalidate();
                // save screen on reference
                Intent serviceIntent = new Intent(this.getApplicationContext(), WriteScreenOnReferenceService.class);
                this.startService(serviceIntent);
                if (stats.hasScreenOffRef()) {
                    // restore any available since screen reference
                    Reference refFrom = ReferenceStore.getReferenceByName(Reference.SCREEN_OFF_REF_FILENAME, this);
                    StatsProvider.getInstance().setCurrentReference(0);
                    // Reference refTo = StatsProvider.getInstance(this).getUncachedPartialReference(0);
                    Reference refTo = ReferenceStore.getReferenceByName(Reference.CURRENT_REF_FILENAME, this);
                    ArrayList<StatElement> otherStats = null;
                    // only process if both references are in the right order
                    if (refFrom.getCreationTime() < refTo.getCreationTime()) {
                        otherStats = stats.getOtherUsageStatList(true, refFrom, false, false, refTo);
                    } else {
                        otherStats = null;
                    }
                    long timeAwake = 0;
                    long timeSince = 0;
                    if ((otherStats != null) && (otherStats.size() > 1)) {
                        timeAwake = ((Misc) stats.getElementByKey(otherStats, StatsProvider.LABEL_MISC_AWAKE)).getTimeOn();
                        timeSince = stats.getBatteryRealtime(StatsProvider.STATS_SCREEN_OFF);
                        Log.i(TAG, "Other stats found. Since=" + timeSince + ", Awake=" + timeAwake);
                    } else {
                        // no stats means the phone was awake
                        timeSince = stats.getBatteryRealtime(StatsProvider.STATS_SCREEN_OFF);
                        timeAwake = timeSince;
                        Log.i(TAG, "Other stats do not have any data. Since=" + timeSince + ", Awake=" + timeAwake);
                    }
                    if (timeSince > 0) {
                        awakePct = (int) ((timeAwake * 100 / timeSince));
                    } else {
                        awakePct = 0;
                    }
                    Log.i(TAG, "Awake %=" + awakePct);
                    // we issue a warning if awakePct > awakeThresholdPct
                    if (awakePct >= awakeThresholdPct) {
                        // Instantiate a Builder object.
                        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
                        String alertText = "";
                        try {
                            alertText = getString(R.string.message_awake_info, awakePct);
                        } catch (Exception e) {
                            alertText = getString(R.string.message_awake_alert);
                        }
                        builder.setSmallIcon(R.drawable.ic_stat_notification);
                        builder.setContentTitle(this.getText(R.string.app_name));
                        builder.setContentText(alertText);
                        // Creates an Intent for the Activity
                        Intent i = new Intent(Intent.ACTION_MAIN);
                        PackageManager manager = this.getPackageManager();
                        i = manager.getLaunchIntentForPackage(this.getPackageName());
                        i.addCategory(Intent.CATEGORY_LAUNCHER);
                        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        i.putExtra(StatsActivity.STAT, 0);
                        i.putExtra(StatsActivity.STAT_TYPE_FROM, Reference.SCREEN_OFF_REF_FILENAME);
                        i.putExtra(StatsActivity.STAT_TYPE_TO, Reference.SCREEN_ON_REF_FILENAME);
                        i.putExtra(StatsActivity.FROM_NOTIFICATION, true);
                        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
                        // Puts the PendingIntent into the notification builder
                        builder.setContentIntent(contentIntent);
                        // Notifications are issued by sending them to the
                        // NotificationManager system service.
                        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                        // Builds an anonymous Notification object from the builder, and
                        // passes it to the NotificationManager
                        mNotificationManager.notify(EventWatcherService.NOTIFICATION_ID, builder.build());
                    }
                }
            } else {
                // delete screen on ref
                ReferenceStore.invalidate(Reference.SCREEN_ON_REF_FILENAME, this);
            }
            // Build the intent to update the widget
            Intent intentRefreshWidgets = new Intent(AppWidget.WIDGET_UPDATE);
            this.sendBroadcast(intentRefreshWidgets);
        }
    } catch (Exception e) {
        Log.e(TAG, "An error occured: " + e.getMessage());
    }
}
Also used : NotificationManager(android.app.NotificationManager) SharedPreferences(android.content.SharedPreferences) Reference(com.asksven.betterbatterystats.data.Reference) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PackageManager(android.content.pm.PackageManager) StatsProvider(com.asksven.betterbatterystats.data.StatsProvider) StatElement(com.asksven.android.common.privateapiproxies.StatElement) NotificationCompat(androidx.core.app.NotificationCompat) PendingIntent(android.app.PendingIntent)

Example 37 with StatElement

use of com.asksven.android.common.privateapiproxies.StatElement in project BetterBatteryStats by asksven.

the class StatsProvider method getCurrentWakelockStatList.

public ArrayList<StatElement> getCurrentWakelockStatList(boolean bFilter, int iPctType, int iSort) throws Exception {
    Context ctx = BbsApplication.getAppContext();
    ArrayList<StatElement> myStats = new ArrayList<StatElement>();
    ArrayList<StatElement> myWakelocks = null;
    BatteryStatsProxy mStats = BatteryStatsProxy.getInstance(ctx);
    int statsType = 0;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        statsType = BatteryStatsTypesLolipop.STATS_CURRENT;
    } else {
        statsType = BatteryStatsTypes.STATS_CURRENT;
    }
    myWakelocks = mStats.getWakelockStats(ctx, BatteryStatsTypes.WAKE_TYPE_PARTIAL, statsType, iPctType);
    ArrayList<Wakelock> myRetWakelocks = new ArrayList<Wakelock>();
    for (int i = 0; i < myWakelocks.size(); i++) {
        Wakelock wl = (Wakelock) myWakelocks.get(i);
        if ((!bFilter) || ((wl.getDuration() / 1000) > 0)) {
            myRetWakelocks.add(wl);
        }
    }
    if (LogSettings.DEBUG) {
        Log.i(TAG, "Result has " + myRetWakelocks.size() + " entries");
    }
    // com.asksven.android.common.privateapiproxies.Walkelock.compareTo
    switch(iSort) {
        case 0:
            // by Duration
            Comparator<Wakelock> myCompTime = new Wakelock.WakelockTimeComparator();
            Collections.sort(myRetWakelocks, myCompTime);
            break;
        case 1:
            // by Count
            Comparator<Wakelock> myCompCount = new Wakelock.WakelockCountComparator();
            Collections.sort(myRetWakelocks, myCompCount);
            break;
    }
    for (int i = 0; i < myRetWakelocks.size(); i++) {
        myStats.add((StatElement) myRetWakelocks.get(i));
    }
    if (LogSettings.DEBUG) {
        Log.d(TAG, "Result " + myStats.toString());
    }
    return myStats;
}
Also used : Context(android.content.Context) Wakelock(com.asksven.android.common.privateapiproxies.Wakelock) NativeKernelWakelock(com.asksven.android.common.privateapiproxies.NativeKernelWakelock) StatElement(com.asksven.android.common.privateapiproxies.StatElement) ArrayList(java.util.ArrayList) BatteryStatsProxy(com.asksven.android.common.privateapiproxies.BatteryStatsProxy) SuppressLint(android.annotation.SuppressLint)

Example 38 with StatElement

use of com.asksven.android.common.privateapiproxies.StatElement in project BetterBatteryStats by asksven.

the class StatsProvider method getElementByKey.

public StatElement getElementByKey(ArrayList<StatElement> myList, String key) {
    Log.i(TAG, "Looking for key: " + key);
    StatElement ret = null;
    if (myList == null) {
        Log.e(TAG, "getElementByKey failed: null list");
        return null;
    }
    for (int i = 0; i < myList.size(); i++) {
        StatElement item = myList.get(i);
        if (item.getName().equals(key)) {
            ret = item;
            break;
        }
    }
    if (ret == null) {
        Log.e(TAG, "getElementByKey failed: " + key + " was not found");
    }
    return ret;
}
Also used : StatElement(com.asksven.android.common.privateapiproxies.StatElement) SuppressLint(android.annotation.SuppressLint)

Example 39 with StatElement

use of com.asksven.android.common.privateapiproxies.StatElement in project BetterBatteryStats by asksven.

the class StatsProvider method getCurrentSensorStatList.

public ArrayList<StatElement> getCurrentSensorStatList(boolean bFilter) throws Exception {
    Context ctx = BbsApplication.getAppContext();
    ArrayList<StatElement> myRetStats = new ArrayList<StatElement>();
    // stop straight away of root features are disabled
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
    BatteryStatsProxy mStats = BatteryStatsProxy.getInstance(ctx);
    int statsType = 0;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        statsType = BatteryStatsTypesLolipop.STATS_CURRENT;
    } else {
        statsType = BatteryStatsTypes.STATS_CURRENT;
    }
    long elapsedRealtime = SystemClock.elapsedRealtime();
    ArrayList<SensorUsage> mySensorStats = mStats.getSensorStats(ctx, elapsedRealtime, statsType);
    ArrayList<SensorUsage> myStats = new ArrayList<SensorUsage>();
    for (int i = 0; i < mySensorStats.size(); i++) {
        SensorUsage sensor = (SensorUsage) mySensorStats.get(i);
        if (sensor != null) {
            if ((!bFilter) || ((sensor.getTotal()) > 0)) {
                myStats.add(sensor);
            }
        }
    }
    Collections.sort(myStats);
    for (int i = 0; i < myStats.size(); i++) {
        myRetStats.add((StatElement) myStats.get(i));
    }
    if (LogSettings.DEBUG) {
        Log.d(TAG, "Result " + myStats.toString());
    }
    return myRetStats;
}
Also used : Context(android.content.Context) SharedPreferences(android.content.SharedPreferences) StatElement(com.asksven.android.common.privateapiproxies.StatElement) ArrayList(java.util.ArrayList) SensorUsage(com.asksven.android.common.privateapiproxies.SensorUsage) BatteryStatsProxy(com.asksven.android.common.privateapiproxies.BatteryStatsProxy) SuppressLint(android.annotation.SuppressLint)

Aggregations

StatElement (com.asksven.android.common.privateapiproxies.StatElement)39 ArrayList (java.util.ArrayList)22 SuppressLint (android.annotation.SuppressLint)21 SharedPreferences (android.content.SharedPreferences)21 Context (android.content.Context)16 Misc (com.asksven.android.common.privateapiproxies.Misc)11 Notification (com.asksven.android.common.privateapiproxies.Notification)10 BatteryStatsProxy (com.asksven.android.common.privateapiproxies.BatteryStatsProxy)9 Reference (com.asksven.betterbatterystats.data.Reference)9 Intent (android.content.Intent)8 PackageManager (android.content.pm.PackageManager)8 Alarm (com.asksven.android.common.privateapiproxies.Alarm)8 NativeKernelWakelock (com.asksven.android.common.privateapiproxies.NativeKernelWakelock)8 StatsProvider (com.asksven.betterbatterystats.data.StatsProvider)8 PendingIntent (android.app.PendingIntent)7 AppWidgetManager (android.appwidget.AppWidgetManager)6 RemoteViews (android.widget.RemoteViews)6 Matcher (java.util.regex.Matcher)6 Pattern (java.util.regex.Pattern)6 NetworkUsage (com.asksven.android.common.privateapiproxies.NetworkUsage)4