use of com.asksven.android.common.privateapiproxies.BatteryStatsProxy in project BetterBatteryStats by asksven.
the class SystemAppActivity method onResume.
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "OnResume called");
final TextView permBATTERY = (TextView) findViewById(R.id.textViewBATTERY_STATS);
final TextView statusBATTERY = (TextView) findViewById(R.id.textViewBATTERY_STATS_STATUS);
final TextView permDUMP = (TextView) findViewById(R.id.textViewDUMP);
final TextView permPACKAGE = (TextView) findViewById(R.id.textViewPACKAGE_USAGE_STATS);
final TextView permAPPOPS = (TextView) findViewById(R.id.textViewAPPOP_USAGE_STATS);
final TextView permACCESS_WIFI_STATE = (TextView) findViewById(R.id.textViewACCESS_WIFI_STATE);
final TextView permACCESS_NETWORK_STATE = (TextView) findViewById(R.id.textViewACCESS_NETWORK_STATE);
final TextView permINTERNET = (TextView) findViewById(R.id.textViewINTERNET);
final TextView permRECEIVE_BOOT_COMPLETED = (TextView) findViewById(R.id.textViewRECEIVE_BOOT_COMPLETED);
final TextView permREAD_PHONE_STATE = (TextView) findViewById(R.id.textViewREAD_PHONE_STATE);
final TextView permBLUETOOTH = (TextView) findViewById(R.id.textViewBLUETOOTH);
final TextView permWAKE_LOCK = (TextView) findViewById(R.id.textViewWAKE_LOCK);
String text = "";
if (SysUtils.hasBatteryStatsPermission(this)) {
permBATTERY.setText("BATTERY_STATS " + getString(R.string.label_granted));
} else {
permBATTERY.setText("BATTERY_STATS " + getString(R.string.label_not_granted));
permBATTERY.setBackgroundColor(Color.RED);
}
BatteryStatsProxy stats = BatteryStatsProxy.getInstance(this);
String status = "";
if (stats.initFailed()) {
status = getString(R.string.label_failed);
if (!stats.getError().equals("")) {
status = status + ": " + stats.getError();
}
} else {
status = getString(R.string.label_success);
if (stats.isFallback()) {
status = status + " (" + getString(R.string.label_fallback) + ")";
}
}
statusBATTERY.setText("STATUS: " + status);
if (SysUtils.hasDumpsysPermission(this)) {
permDUMP.setText("DUMP " + getString(R.string.label_granted));
} else {
permDUMP.setText("DUMP " + getString(R.string.label_not_granted));
permDUMP.setBackgroundColor(Color.RED);
}
if (Build.VERSION.SDK_INT >= 21) {
if (SysUtils.hasPackageUsageStatsPermission(this)) {
permPACKAGE.setText("PACKAGE_USAGE_STATS " + getString(R.string.label_granted));
// permPACKAGE.setBackgroundColor(Color.WHITE);
} else {
permPACKAGE.setText("PACKAGE_USAGE_STATS " + getString(R.string.label_not_granted));
permPACKAGE.setBackgroundColor(Color.RED);
}
} else {
permPACKAGE.setText("PACKAGE_USAGE_STATS " + getString(R.string.label_not_needed));
}
if (Build.VERSION.SDK_INT >= 21) {
checkAndRequestPermissionAppOpsUsageStats();
if (SystemAppActivity.hasPermissionAppOpsUsageStats(this)) {
permAPPOPS.setText("APPOPS_USAGE_STATS " + getString(R.string.label_granted));
// permAPPOPS.setBackgroundColor(Color.WHITE);
} else {
permAPPOPS.setText("APPOPS_USAGE_STATS " + getString(R.string.label_not_granted));
permAPPOPS.setBackgroundColor(Color.RED);
}
} else {
permAPPOPS.setText("APPOPS_USAGE_STATS " + getString(R.string.label_not_needed));
}
checkAndRequestPermission(Manifest.permission.ACCESS_WIFI_STATE, CONST_ACCESS_WIFI_STATE, getString(R.string.perm_rationale_ACCESS_WIFI_STATE));
if (SystemAppActivity.hasPermission(Manifest.permission.ACCESS_WIFI_STATE, this)) {
permACCESS_WIFI_STATE.setText("ACCESS_WIFI_STATE " + getString(R.string.label_granted));
} else {
permACCESS_WIFI_STATE.setText("ACCESS_WIFI_STATE " + getString(R.string.label_not_granted));
permACCESS_WIFI_STATE.setBackgroundColor(Color.RED);
}
checkAndRequestPermission(Manifest.permission.ACCESS_NETWORK_STATE, CONST_ACCESS_NETWORK_STATE, getString(R.string.perm_rationale_ACCESS_NETWORK_STATE));
if (SystemAppActivity.hasPermission(Manifest.permission.ACCESS_NETWORK_STATE, this)) {
permACCESS_NETWORK_STATE.setText("ACCESS_NETWORK_STATE " + getString(R.string.label_granted));
} else {
permACCESS_NETWORK_STATE.setText("ACCESS_NETWORK_STATE " + getString(R.string.label_not_granted));
permACCESS_NETWORK_STATE.setBackgroundColor(Color.RED);
}
checkAndRequestPermission(Manifest.permission.INTERNET, CONST_INTERNET, getString(R.string.perm_rationale_INTERNET));
if (SystemAppActivity.hasPermission(Manifest.permission.INTERNET, this)) {
permINTERNET.setText("INTERNET " + getString(R.string.label_granted));
} else {
permINTERNET.setText("INTERNET " + getString(R.string.label_not_granted));
permINTERNET.setBackgroundColor(Color.RED);
}
checkAndRequestPermission(Manifest.permission.RECEIVE_BOOT_COMPLETED, CONST_RECEIVE_BOOT_COMPLETED, getString(R.string.perm_rationale_RECEIVE_BOOT_COMPLETED));
if (SystemAppActivity.hasPermission(Manifest.permission.RECEIVE_BOOT_COMPLETED, this)) {
permRECEIVE_BOOT_COMPLETED.setText("RECEIVE_BOOT_COMPLETED " + getString(R.string.label_granted));
} else {
permRECEIVE_BOOT_COMPLETED.setText("RECEIVE_BOOT_COMPLETED " + getString(R.string.label_not_granted));
permRECEIVE_BOOT_COMPLETED.setBackgroundColor(Color.RED);
}
checkAndRequestPermission(Manifest.permission.READ_PHONE_STATE, CONST_READ_PHONE_STATE, getString(R.string.perm_rationale_READ_PHONE_STATE));
if (SystemAppActivity.hasPermission(Manifest.permission.READ_PHONE_STATE, this)) {
permREAD_PHONE_STATE.setText("READ_PHONE_STATE " + getString(R.string.label_granted));
} else {
permREAD_PHONE_STATE.setText("READ_PHONE_STATE " + getString(R.string.label_not_granted));
permREAD_PHONE_STATE.setBackgroundColor(Color.RED);
}
checkAndRequestPermission(Manifest.permission.BLUETOOTH, CONST_BLUETOOTH, "");
if (SystemAppActivity.hasPermission(Manifest.permission.BLUETOOTH, this)) {
permBLUETOOTH.setText("BLUETOOTH " + getString(R.string.label_granted));
} else {
permBLUETOOTH.setText("BLUETOOTH " + getString(R.string.label_not_granted));
permBLUETOOTH.setBackgroundColor(Color.RED);
}
checkAndRequestPermission(Manifest.permission.WAKE_LOCK, CONST_WAKE_LOCK, "");
if (SystemAppActivity.hasPermission(Manifest.permission.WAKE_LOCK, this)) {
permWAKE_LOCK.setText("WAKE_LOCK " + getString(R.string.label_granted));
} else {
permWAKE_LOCK.setText("WAKE_LOCK " + getString(R.string.label_not_granted));
permWAKE_LOCK.setBackgroundColor(Color.RED);
}
}
use of com.asksven.android.common.privateapiproxies.BatteryStatsProxy in project BetterBatteryStats by asksven.
the class UpdateWidgetService method onHandleWork.
@Override
protected void onHandleWork(Intent intent) {
// We have received work to do. The system or framework is already
// holding a wake lock for us at this point, so we can just go.
Log.i(TAG, "onHandleWork: " + intent);
if (LogSettings.DEBUG) {
Log.d(TAG, "Service started");
}
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
StatsProvider stats = StatsProvider.getInstance();
// make sure to flush cache
BatteryStatsProxy proxy = BatteryStatsProxy.getInstance(this);
if (proxy != null) {
proxy.invalidate();
}
if (allWidgetIds != null) {
if (allWidgetIds.length == 0) {
Log.i(TAG, "allWidgetIds was empty");
}
for (int widgetId : allWidgetIds) {
Log.i(TAG, "Update widget " + widgetId);
RemoteViews remoteViews = new RemoteViews(this.getApplicationContext().getPackageName(), R.layout.widget);
final int cellSize = 40;
int width = 3;
int height = 2;
int widthDim = 0;
int heightDim = 0;
if (Build.VERSION.SDK_INT >= 16) {
Bundle widgetOptions = appWidgetManager.getAppWidgetOptions(widgetId);
width = AppWidget.sizeToCells(widgetOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH) - 10);
height = AppWidget.sizeToCells(widgetOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT) + 10);
widthDim = widgetOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
heightDim = widgetOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);
Log.i(TAG, "[" + widgetId + "] height=" + height + " (" + widgetOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT) + ")");
Log.i(TAG, "[" + widgetId + "] width=" + width + "(" + widgetOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH) + ")");
remoteViews = new RemoteViews(this.getPackageName(), R.layout.widget_horz);
if ((height <= 2) && (width <= 2)) {
// switch to image only
Log.i(TAG, "[" + widgetId + "] using image-only layout");
remoteViews = new RemoteViews(this.getPackageName(), R.layout.widget);
} else if (height < width) {
// switch to horizontal
Log.i(TAG, "[" + widgetId + "] using horizontal layout");
remoteViews = new RemoteViews(this.getPackageName(), R.layout.widget_horz);
} else {
// switch to vertical
Log.i(TAG, "[" + widgetId + "] using vertical layout");
remoteViews = new RemoteViews(this.getPackageName(), R.layout.widget_vert);
}
}
// we change the bg color of the layout based on alpha from prefs
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
int opacity = sharedPrefs.getInt("new_widget_bg_opacity", 20);
opacity = (255 * opacity) / 100;
remoteViews.setInt(R.id.background, "setBackgroundColor", (opacity << 24) & android.graphics.Color.BLACK);
// remoteViews.setInt(R.id.layoutBackground, "setImageAlpha", opacity);
long timeAwake = 0;
long timeSince = 0;
long timeScreenOn = 0;
long timeDeepSleep = 0;
long timePWL = 0;
long timeKWL = 0;
String refFrom = sharedPrefs.getString("new_widget_default_stat_type", Reference.UNPLUGGED_REF_FILENAME);
try {
// retrieve stats
Reference currentRef = StatsProvider.getInstance().getUncachedPartialReference(0);
Reference fromRef = ReferenceStore.getReferenceByName(refFrom, this);
ArrayList<StatElement> otherStats = stats.getOtherUsageStatList(true, fromRef, false, true, currentRef);
if ((otherStats == null) || (otherStats.size() == 1)) {
// the desired stat type is unavailable, pick the alternate one and go on with that one
refFrom = sharedPrefs.getString("widget_fallback_stat_type", Reference.UNPLUGGED_REF_FILENAME);
fromRef = ReferenceStore.getReferenceByName(refFrom, this);
otherStats = stats.getOtherUsageStatList(true, fromRef, false, true, currentRef);
}
timeSince = StatsProvider.getInstance().getSince(fromRef, currentRef);
if ((otherStats != null) && (otherStats.size() > 1)) {
Misc timeAwakeStat = (Misc) stats.getElementByKey(otherStats, StatsProvider.LABEL_MISC_AWAKE);
if (timeAwakeStat != null) {
timeAwake = timeAwakeStat.getTimeOn();
} else {
timeAwake = 0;
}
Misc timeScreenOnStat = (Misc) stats.getElementByKey(otherStats, "Screen On");
if (timeScreenOnStat != null) {
timeScreenOn = timeScreenOnStat.getTimeOn();
} else {
timeScreenOn = 0;
}
Misc deepSleepStat = ((Misc) stats.getElementByKey(otherStats, "Deep Sleep"));
if (deepSleepStat != null) {
timeDeepSleep = deepSleepStat.getTimeOn();
} else {
timeDeepSleep = 0;
}
ArrayList<StatElement> pWakelockStats = stats.getWakelockStatList(true, fromRef, 0, 0, currentRef);
timePWL = stats.sum(pWakelockStats);
ArrayList<StatElement> kWakelockStats = stats.getKernelWakelockStatList(true, fromRef, 0, 0, currentRef);
timeKWL = stats.sum(kWakelockStats);
} else {
}
} catch (Exception e) {
Log.e(TAG, "Exception: " + Log.getStackTraceString(e));
} finally {
if (LogSettings.DEBUG) {
Log.d(TAG, "Reference: " + refFrom);
Log.d(TAG, "Since: " + DateUtils.formatShort(timeSince) + " " + AppWidget.formatDuration(timeSince) + " " + timeSince);
Log.d(TAG, "Awake: " + DateUtils.formatShort(timeAwake) + " " + AppWidget.formatDuration(timeAwake) + " " + timeAwake);
Log.d(TAG, "Screen on: " + DateUtils.formatShort(timeScreenOn) + " " + AppWidget.formatDuration(timeScreenOn) + " " + timeScreenOn);
Log.d(TAG, "Deep sleep: " + DateUtils.formatShort(timeDeepSleep) + " " + AppWidget.formatDuration(timeDeepSleep) + " " + timeDeepSleep);
Log.d(TAG, "KWL: " + DateUtils.formatShort(timeKWL) + " " + AppWidget.formatDuration(timeKWL) + " " + timeKWL);
Log.d(TAG, "PWL: " + DateUtils.formatShort(timePWL) + " " + AppWidget.formatDuration(timePWL) + " " + timePWL);
}
WidgetSummary graph = new WidgetSummary();
graph.setAwake(timeAwake);
graph.setScreenOn(timeScreenOn);
graph.setDeepSleep(timeDeepSleep);
graph.setDuration(timeSince);
graph.setKWL(timeKWL);
graph.setPWL(timePWL);
DisplayMetrics metrics = this.getResources().getDisplayMetrics();
Log.i(TAG, "Widget Dimensions: height=" + heightDim + " width=" + widthDim);
Float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, Math.min(Math.max(Math.min(widthDim, heightDim), 80), 160), metrics);
Log.i(TAG, "BitmapDip=" + Math.min(Math.max(Math.min(widthDim, heightDim), 80), 160) + ", BitmapPx=" + px.intValue());
graph.setBitmapSizePx(px.intValue());
remoteViews.setImageViewBitmap(R.id.imageView1, graph.getBitmap(this));
boolean show_pwc_only = sharedPrefs.getBoolean("widget_show_pct", false);
if (show_pwc_only) {
UpdateWidgetService.setValuesToPct(remoteViews, timeAwake, timeSince, timeScreenOn, timeDeepSleep, timePWL, timeKWL);
} else {
if ((width > height) && (width <= 4)) {
UpdateWidgetService.setValuesToDuration(remoteViews, timeAwake, timeSince, timeScreenOn, timeDeepSleep, timePWL, timeKWL);
} else {
UpdateWidgetService.setValuesToDurationAndPct(remoteViews, timeAwake, timeSince, timeScreenOn, timeDeepSleep, timePWL, timeKWL);
}
}
boolean showColor = sharedPrefs.getBoolean("text_widget_color", true);
UpdateWidgetService.setTextColor(remoteViews, showColor, this);
// tap zones
// Register an onClickListener for the graph -> refresh
Intent clickIntentRefresh = new Intent(this.getApplicationContext(), AppWidget.class);
clickIntentRefresh.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
clickIntentRefresh.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
PendingIntent pendingIntentRefresh = PendingIntent.getBroadcast(getApplicationContext(), 0, clickIntentRefresh, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.imageViewRefresh, pendingIntentRefresh);
// Register an onClickListener for the widget -> call main activity
Intent i = new Intent(Intent.ACTION_MAIN);
PackageManager manager = getPackageManager();
i = manager.getLaunchIntentForPackage(getPackageName());
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
int stat = Integer.valueOf(sharedPrefs.getString("widget_default_stat", "0"));
i.putExtra(StatsActivity.STAT, stat);
i.putExtra(StatsActivity.STAT_TYPE_FROM, refFrom);
i.putExtra(StatsActivity.STAT_TYPE_TO, Reference.CURRENT_REF_FILENAME);
PendingIntent clickPI = PendingIntent.getActivity(this.getApplicationContext(), PI_CODE, i, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.imageView1, clickPI);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
} else {
Log.i(TAG, "allWidgetIds was null");
}
Log.i(TAG, "Completed service @ " + DateUtils.formatDurationLong(SystemClock.elapsedRealtime()));
}
use of com.asksven.android.common.privateapiproxies.BatteryStatsProxy in project BetterBatteryStats by asksven.
the class StatsProvider method getCurrentKernelWakelockStatList.
public ArrayList<StatElement> getCurrentKernelWakelockStatList(boolean bFilter, int iPctType, int iSort) throws Exception {
Context ctx = BbsApplication.getAppContext();
ArrayList<StatElement> myStats = new ArrayList<StatElement>();
ArrayList<StatElement> myKernelWakelocks = null;
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;
}
myKernelWakelocks = mStats.getKernelWakelockStats(ctx, statsType, false);
ArrayList<NativeKernelWakelock> myRetKernelWakelocks = new ArrayList<NativeKernelWakelock>();
for (int i = 0; i < myKernelWakelocks.size(); i++) {
NativeKernelWakelock wl = (NativeKernelWakelock) myKernelWakelocks.get(i);
if ((!bFilter) || ((wl.getDuration()) > 0)) {
myRetKernelWakelocks.add(wl);
}
}
// com.asksven.android.common.privateapiproxies.Walkelock.compareTo
switch(iSort) {
case 0:
// by Duration
Comparator<NativeKernelWakelock> myCompTime = new NativeKernelWakelock.TimeComparator();
Collections.sort(myRetKernelWakelocks, myCompTime);
break;
case 1:
// by Count
Comparator<NativeKernelWakelock> myCompCount = new NativeKernelWakelock.CountComparator();
Collections.sort(myRetKernelWakelocks, myCompCount);
break;
}
for (int i = 0; i < myRetKernelWakelocks.size(); i++) {
myStats.add((StatElement) myRetKernelWakelocks.get(i));
}
if (LogSettings.DEBUG) {
Log.d(TAG, "Result " + myStats.toString());
}
return myStats;
}
use of com.asksven.android.common.privateapiproxies.BatteryStatsProxy in project BetterBatteryStats by asksven.
the class StatsProvider method testAPI.
public static void testAPI() {
// test against BatteryStatsProxy
BatteryStatsProxy mStats = BatteryStatsProxy.getInstance(BbsApplication.getAppContext());
long rawRealtime = SystemClock.elapsedRealtime() * 1000;
long batteryRealtime = 0;
Long res = 0L;
try {
batteryRealtime = mStats.getBatteryRealtime(rawRealtime);
if (batteryRealtime > 0) {
Log.i(TAG_TEST, "Passed: getBatteryRealtime");
} else {
Log.e(TAG_TEST, "FAILED: getBatteryRealtime");
}
if (Build.VERSION.SDK_INT < 6) {
res = mStats.getBluetoothOnTime(batteryRealtime, getStatsType());
if (res > 0) {
Log.i(TAG_TEST, "Passed: getBluetoothOnTime");
} else {
Log.e(TAG_TEST, "FAILED: getBluetoothOnTime");
}
}
res = mStats.getSensorOnTime(BbsApplication.getAppContext(), batteryRealtime, getStatsType());
if (res > 0) {
Log.i(TAG_TEST, "Passed: getSensorOnTime");
} else {
Log.e(TAG_TEST, "FAILED: getSensorOnTime");
}
if (Build.VERSION.SDK_INT >= 6) {
res = mStats.getSyncOnTime(BbsApplication.getAppContext(), batteryRealtime, getStatsType());
if (res > 0) {
Log.i(TAG_TEST, "Passed: getSyncOnTime");
} else {
Log.e(TAG_TEST, "FAILED: getSyncOnTime");
}
}
} catch (Exception e) {
Log.e(TAG_TEST, "Test threw exception: " + e.getMessage());
}
}
use of com.asksven.android.common.privateapiproxies.BatteryStatsProxy in project BetterBatteryStats by asksven.
the class StatsProvider method getBatteryRealtime.
/**
* Returns the battery realtime since a given reference
*
* @param iStatType
* the reference
* @return the battery realtime
*/
public long getBatteryRealtime(int iStatType) throws BatteryInfoUnavailableException {
Context ctx = BbsApplication.getAppContext();
long rawRealtime = SystemClock.elapsedRealtime() * 1000;
long whichRealtime = 0;
int statsType = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
statsType = BatteryStatsTypesLolipop.STATS_CURRENT;
} else {
statsType = BatteryStatsTypes.STATS_CURRENT;
}
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
if (!SysUtils.hasBatteryStatsPermission(ctx)) {
whichRealtime = rawRealtime;
return whichRealtime;
}
BatteryStatsProxy mStats = BatteryStatsProxy.getInstance(ctx);
whichRealtime = mStats.computeBatteryRealtime(rawRealtime, statsType) / 1000;
if ((iStatType == StatsProvider.STATS_CUSTOM) && (ReferenceStore.getReferenceByName(Reference.CUSTOM_REF_FILENAME, ctx) != null)) {
whichRealtime -= ReferenceStore.getReferenceByName(Reference.CUSTOM_REF_FILENAME, ctx).m_refBatteryRealtime;
} else if ((iStatType == StatsProvider.STATS_SCREEN_OFF) && (ReferenceStore.getReferenceByName(Reference.SCREEN_OFF_REF_FILENAME, ctx) != null)) {
whichRealtime -= ReferenceStore.getReferenceByName(Reference.SCREEN_OFF_REF_FILENAME, ctx).m_refBatteryRealtime;
} else if ((iStatType == StatsProvider.STATS_BOOT) && (ReferenceStore.getReferenceByName(Reference.BOOT_REF_FILENAME, ctx) != null)) {
whichRealtime -= ReferenceStore.getReferenceByName(Reference.BOOT_REF_FILENAME, ctx).m_refBatteryRealtime;
}
Log.i(TAG, "rawRealtime = " + rawRealtime);
Log.i(TAG, "whichRealtime = " + whichRealtime);
return whichRealtime;
}
Aggregations