Merge "Not using reflection for app-usage API" into ub-launcher3-master

This commit is contained in:
Vadim Tryshev 2019-03-11 20:25:28 +00:00 committed by Android (Google) Code Review
commit 51f3759c34
1 changed files with 15 additions and 31 deletions

View File

@ -23,6 +23,7 @@ import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.LauncherApps;
import android.content.pm.LauncherApps.AppUsageLimit;
import android.content.res.Resources;
import android.icu.text.MeasureFormat;
import android.icu.text.MeasureFormat.FormatWidth;
@ -44,13 +45,13 @@ import com.android.launcher3.Utilities;
import com.android.launcher3.userevent.nano.LauncherLogProto;
import com.android.systemui.shared.recents.model.Task;
import java.lang.reflect.Method;
import java.time.Duration;
import java.util.Locale;
public final class DigitalWellBeingToast extends LinearLayout {
static final Intent OPEN_APP_USAGE_SETTINGS_TEMPLATE = new Intent(ACTION_APP_USAGE_SETTINGS);
static final int MINUTE_MS = 60000;
private final LauncherApps mLauncherApps;
public interface InitializeCallback {
void call(float saturation, String contentDescription);
@ -67,6 +68,7 @@ public final class DigitalWellBeingToast extends LinearLayout {
setLayoutDirection(Utilities.isRtl(getResources()) ?
View.LAYOUT_DIRECTION_RTL : View.LAYOUT_DIRECTION_LTR);
setOnClickListener((view) -> openAppUsageSettings());
mLauncherApps = context.getSystemService(LauncherApps.class);
}
@Override
@ -87,47 +89,29 @@ public final class DigitalWellBeingToast extends LinearLayout {
}
Utilities.THREAD_POOL_EXECUTOR.execute(() -> {
long appUsageLimitTimeMs = -1;
long appRemainingTimeMs = -1;
final AppUsageLimit usageLimit = mLauncherApps.getAppUsageLimit(
task.getTopComponent().getPackageName(),
UserHandle.of(task.key.userId));
try {
final Method getAppUsageLimit = LauncherApps.class.getMethod(
"getAppUsageLimit",
String.class,
UserHandle.class);
final Object usageLimit = getAppUsageLimit.invoke(
getContext().getSystemService(LauncherApps.class),
task.getTopComponent().getPackageName(),
UserHandle.of(task.key.userId));
if (usageLimit != null) {
final Class appUsageLimitClass = usageLimit.getClass();
appUsageLimitTimeMs = (long) appUsageLimitClass.getMethod("getTotalUsageLimit").
invoke(usageLimit);
appRemainingTimeMs = (long) appUsageLimitClass.getMethod("getUsageRemaining").
invoke(usageLimit);
}
} catch (Exception e) {
// Do nothing
}
final long appUsageLimitTimeMsFinal = appUsageLimitTimeMs;
final long appRemainingTimeMsFinal = appRemainingTimeMs;
final long appUsageLimitTimeMs =
usageLimit != null ? usageLimit.getTotalUsageLimit() : -1;
final long appRemainingTimeMs =
usageLimit != null ? usageLimit.getUsageRemaining() : -1;
post(() -> {
if (appUsageLimitTimeMsFinal < 0) {
if (appUsageLimitTimeMs < 0) {
setVisibility(GONE);
} else {
setVisibility(VISIBLE);
mText.setText(getText(appRemainingTimeMsFinal));
mImage.setImageResource(appRemainingTimeMsFinal > 0 ?
mText.setText(getText(appRemainingTimeMs));
mImage.setImageResource(appRemainingTimeMs > 0 ?
R.drawable.hourglass_top : R.drawable.hourglass_bottom);
}
callback.call(
appUsageLimitTimeMsFinal >= 0 && appRemainingTimeMsFinal <= 0 ? 0 : 1,
appUsageLimitTimeMs >= 0 && appRemainingTimeMs <= 0 ? 0 : 1,
getContentDescriptionForTask(
task, appUsageLimitTimeMsFinal, appRemainingTimeMsFinal));
task, appUsageLimitTimeMs, appRemainingTimeMs));
});
});
}