Settings: Improve code for time spent in app
Change-Id: I3b1db9b6f4918b1ea770c0ec0c89e22119e3e6b1 Signed-off-by: Jyotiraditya Panda <jyotiraditya@aospa.co> Signed-off-by: Pranav Vashi <neobuddy89@gmail.com>
This commit is contained in:
@@ -60,6 +60,10 @@ public class ApplicationFeatureProviderImpl implements ApplicationFeatureProvide
|
|||||||
private static final String TAG = "AppFeatureProviderImpl";
|
private static final String TAG = "AppFeatureProviderImpl";
|
||||||
private static final boolean DEBUG = false;
|
private static final boolean DEBUG = false;
|
||||||
|
|
||||||
|
private static final String WELLBEING_APP_PACKAGE = "com.google.android.apps.wellbeing.api";
|
||||||
|
private static final String GET_APP_USAGE_MILLIS = "get_app_usage_millis";
|
||||||
|
private static final String TOTAL_TIME_MILLIS = "total_time_millis";
|
||||||
|
|
||||||
protected final Context mContext;
|
protected final Context mContext;
|
||||||
private final PackageManager mPm;
|
private final PackageManager mPm;
|
||||||
private final IPackageManager mPms;
|
private final IPackageManager mPms;
|
||||||
@@ -376,31 +380,30 @@ public class ApplicationFeatureProviderImpl implements ApplicationFeatureProvide
|
|||||||
|
|
||||||
public CharSequence getTimeSpentInApp(String packageName) {
|
public CharSequence getTimeSpentInApp(String packageName) {
|
||||||
try {
|
try {
|
||||||
if (!isPrivilegedApp("com.google.android.apps.wellbeing.api")) {
|
if (!isPrivilegedApp(WELLBEING_APP_PACKAGE)) {
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
Log.d("ApplicationFeatureProviderImpl", "Not a privileged app.");
|
Log.d("ApplicationFeatureProviderImpl", "Not a privileged app.");
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
Bundle bundle = new Bundle();
|
Bundle bundle = new Bundle();
|
||||||
bundle.putString("packageName", packageName);
|
bundle.putString("packageName", packageName);
|
||||||
Bundle call = mContext.getContentResolver().call(
|
|
||||||
"com.google.android.apps.wellbeing.api",
|
Bundle providerCall = mContext.getContentResolver().call(WELLBEING_APP_PACKAGE,
|
||||||
"get_app_usage_millis", null, bundle);
|
GET_APP_USAGE_MILLIS, null, bundle);
|
||||||
if (call != null) {
|
if (providerCall != null) {
|
||||||
if (call.getBoolean("success")) {
|
if (providerCall.getBoolean("success")) {
|
||||||
Bundle bundle2 = call.getBundle("data");
|
Bundle dataBundle = providerCall.getBundle("data");
|
||||||
if (bundle2 == null) {
|
if (dataBundle == null) {
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
Log.d("ApplicationFeatureProviderImpl", "data bundle is null.");
|
Log.d("ApplicationFeatureProviderImpl", "data bundle is null.");
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
String readableDuration = getReadableDuration(
|
String readableDuration = getReadableDuration(dataBundle.getLong(TOTAL_TIME_MILLIS),
|
||||||
Long.valueOf(bundle2.getLong("total_time_millis")),
|
R.string.duration_less_than_one_minute);
|
||||||
FormatWidth.NARROW, R.string.duration_less_than_one_minute, false);
|
return mContext.getString(R.string.screen_time_summary_usage_today, readableDuration);
|
||||||
return mContext.getString(
|
|
||||||
R.string.screen_time_summary_usage_today, readableDuration);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
@@ -408,62 +411,51 @@ public class ApplicationFeatureProviderImpl implements ApplicationFeatureProvide
|
|||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.w("ApplicationFeatureProviderImpl", "Error getting time spent for app " + packageName, e);
|
Log.w("ApplicationFeatureProviderImpl",
|
||||||
|
"Error getting time spent for app " + packageName, e);
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String getReadableDuration(Long totalTime, FormatWidth formatWidth, int defaultString, boolean landscape) {
|
private String getReadableDuration(Long totalTime, int defaultString) {
|
||||||
long j;
|
long hours, minutes;
|
||||||
long j2;
|
|
||||||
long longValue = totalTime.longValue();
|
if (totalTime >= 3_600_000) {
|
||||||
if (longValue >= 3600000) {
|
hours = totalTime / 3_600_000;
|
||||||
j = longValue / 3600000;
|
totalTime -= 3_600_000 * hours;
|
||||||
longValue -= 3600000 * j;
|
|
||||||
} else {
|
} else {
|
||||||
j = 0;
|
hours = 0;
|
||||||
}
|
}
|
||||||
if (longValue >= 60000) {
|
|
||||||
j2 = longValue / 60000;
|
if (totalTime >= 60_000) {
|
||||||
longValue -= 60000 * j2;
|
minutes = totalTime / 60_000;
|
||||||
|
totalTime -= 60_000 * minutes;
|
||||||
} else {
|
} else {
|
||||||
j2 = 0;
|
minutes = 0;
|
||||||
}
|
}
|
||||||
int i2 = (j > 0 ? 1 : (j == 0 ? 0 : -1));
|
|
||||||
if (i2 > 0 && j2 > 0) {
|
Locale locale = Locale.getDefault();
|
||||||
return MeasureFormat.getInstance(Locale.getDefault(), formatWidth).formatMeasures(
|
MeasureFormat measureFormat = MeasureFormat.getInstance(locale, FormatWidth.NARROW);
|
||||||
new Measure(Long.valueOf(j), MeasureUnit.HOUR),
|
|
||||||
new Measure(Long.valueOf(j2), MeasureUnit.MINUTE));
|
if (hours > 0 && minutes > 0) {
|
||||||
} else if (i2 > 0) {
|
return measureFormat.formatMeasures(new Measure(hours, MeasureUnit.HOUR),
|
||||||
Locale locale = Locale.getDefault();
|
new Measure(minutes, MeasureUnit.MINUTE));
|
||||||
if (!landscape) {
|
} else if (hours > 0) {
|
||||||
formatWidth = FormatWidth.WIDE;
|
return measureFormat.formatMeasures(new Measure(hours, MeasureUnit.HOUR));
|
||||||
}
|
} else if (minutes > 0) {
|
||||||
return MeasureFormat.getInstance(locale, formatWidth).formatMeasures(
|
return measureFormat.formatMeasures(new Measure(minutes, MeasureUnit.MINUTE));
|
||||||
new Measure(Long.valueOf(j), MeasureUnit.HOUR));
|
} else if (totalTime <= 0) {
|
||||||
} else if (j2 > 0) {
|
return measureFormat.formatMeasures(new Measure(0, MeasureUnit.MINUTE));
|
||||||
Locale locale2 = Locale.getDefault();
|
|
||||||
if (!landscape) {
|
|
||||||
formatWidth = FormatWidth.WIDE;
|
|
||||||
}
|
|
||||||
return MeasureFormat.getInstance(locale2, formatWidth).formatMeasures(
|
|
||||||
new Measure(Long.valueOf(j2), MeasureUnit.MINUTE));
|
|
||||||
} else if (longValue > 0) {
|
|
||||||
return mContext.getResources().getString(defaultString);
|
|
||||||
} else {
|
|
||||||
Locale locale3 = Locale.getDefault();
|
|
||||||
if (!landscape) {
|
|
||||||
formatWidth = FormatWidth.WIDE;
|
|
||||||
}
|
|
||||||
return MeasureFormat.getInstance(locale3, formatWidth).formatMeasures(
|
|
||||||
new Measure(0, MeasureUnit.MINUTE));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return mContext.getResources().getString(defaultString);
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean isPrivilegedApp(String packageName) {
|
private boolean isPrivilegedApp(String packageName) {
|
||||||
ProviderInfo resolveContentProvider = mContext.getPackageManager().resolveContentProvider(packageName, 0);
|
ProviderInfo providerInfo = mContext.getPackageManager().resolveContentProvider(packageName, 0);
|
||||||
if (resolveContentProvider == null)
|
if (providerInfo != null) {
|
||||||
return false;
|
return providerInfo.applicationInfo.isPrivilegedApp();
|
||||||
return resolveContentProvider.applicationInfo.isPrivilegedApp();
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user