Use ThreadUtils instead of AsyncTask to get app title/icon
Change-Id: I44459d9e4a11329a3bd3bfda5057e05eb7e04d6a Fixes: 63620905 Test: robotest
This commit is contained in:
@@ -15,38 +15,46 @@
|
|||||||
package com.android.settings.datausage;
|
package com.android.settings.datausage;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.AsyncTask;
|
|
||||||
import android.support.v7.preference.Preference;
|
import android.support.v7.preference.Preference;
|
||||||
import android.support.v7.preference.PreferenceViewHolder;
|
import android.support.v7.preference.PreferenceViewHolder;
|
||||||
import android.text.format.Formatter;
|
import android.text.format.Formatter;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.ProgressBar;
|
import android.widget.ProgressBar;
|
||||||
|
|
||||||
|
import com.android.settings.R;
|
||||||
import com.android.settingslib.AppItem;
|
import com.android.settingslib.AppItem;
|
||||||
import com.android.settingslib.net.UidDetail;
|
import com.android.settingslib.net.UidDetail;
|
||||||
import com.android.settingslib.net.UidDetailProvider;
|
import com.android.settingslib.net.UidDetailProvider;
|
||||||
|
import com.android.settingslib.utils.ThreadUtils;
|
||||||
import static com.android.internal.util.Preconditions.checkNotNull;
|
|
||||||
|
|
||||||
public class AppDataUsagePreference extends Preference {
|
public class AppDataUsagePreference extends Preference {
|
||||||
|
|
||||||
private final AppItem mItem;
|
private final AppItem mItem;
|
||||||
private final int mPercent;
|
private final int mPercent;
|
||||||
|
private UidDetail mDetail;
|
||||||
|
|
||||||
public AppDataUsagePreference(Context context, AppItem item, int percent,
|
public AppDataUsagePreference(Context context, AppItem item, int percent,
|
||||||
UidDetailProvider provider) {
|
UidDetailProvider provider) {
|
||||||
super(context);
|
super(context);
|
||||||
mItem = item;
|
mItem = item;
|
||||||
mPercent = percent;
|
mPercent = percent;
|
||||||
setLayoutResource(com.android.settings.R.layout.data_usage_item);
|
setLayoutResource(R.layout.data_usage_item);
|
||||||
setWidgetLayoutResource(com.android.settings.R.layout.widget_progress_bar);
|
setWidgetLayoutResource(R.layout.widget_progress_bar);
|
||||||
|
|
||||||
if (item.restricted && item.total <= 0) {
|
if (item.restricted && item.total <= 0) {
|
||||||
setSummary(com.android.settings.R.string.data_usage_app_restricted);
|
setSummary(com.android.settings.R.string.data_usage_app_restricted);
|
||||||
} else {
|
} else {
|
||||||
setSummary(Formatter.formatFileSize(context, item.total));
|
setSummary(Formatter.formatFileSize(context, item.total));
|
||||||
}
|
}
|
||||||
|
mDetail = provider.getUidDetail(item.key, false /* blocking */);
|
||||||
// kick off async load of app details
|
if (mDetail != null) {
|
||||||
UidDetailTask.bindView(provider, item, this);
|
setAppInfo();
|
||||||
|
} else {
|
||||||
|
ThreadUtils.postOnBackgroundThread(() -> {
|
||||||
|
mDetail = provider.getUidDetail(mItem.key, true /* blocking */);
|
||||||
|
ThreadUtils.postOnMainThread(() -> setAppInfo());
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -64,59 +72,17 @@ public class AppDataUsagePreference extends Preference {
|
|||||||
progress.setProgress(mPercent);
|
progress.setProgress(mPercent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setAppInfo() {
|
||||||
|
if (mDetail != null) {
|
||||||
|
setIcon(mDetail.icon);
|
||||||
|
setTitle(mDetail.label);
|
||||||
|
} else {
|
||||||
|
setIcon(null);
|
||||||
|
setTitle(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public AppItem getItem() {
|
public AppItem getItem() {
|
||||||
return mItem;
|
return mItem;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Background task that loads {@link UidDetail}, binding to
|
|
||||||
* {@link DataUsageAdapter} row item when finished.
|
|
||||||
*/
|
|
||||||
private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
|
|
||||||
private final UidDetailProvider mProvider;
|
|
||||||
private final AppItem mItem;
|
|
||||||
private final AppDataUsagePreference mTarget;
|
|
||||||
|
|
||||||
private UidDetailTask(UidDetailProvider provider, AppItem item,
|
|
||||||
AppDataUsagePreference target) {
|
|
||||||
mProvider = checkNotNull(provider);
|
|
||||||
mItem = checkNotNull(item);
|
|
||||||
mTarget = checkNotNull(target);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void bindView(UidDetailProvider provider, AppItem item,
|
|
||||||
AppDataUsagePreference target) {
|
|
||||||
final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
|
|
||||||
if (cachedDetail != null) {
|
|
||||||
bindView(cachedDetail, target);
|
|
||||||
} else {
|
|
||||||
new UidDetailTask(provider, item, target).execute();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void bindView(UidDetail detail, Preference target) {
|
|
||||||
if (detail != null) {
|
|
||||||
target.setIcon(detail.icon);
|
|
||||||
target.setTitle(detail.label);
|
|
||||||
} else {
|
|
||||||
target.setIcon(null);
|
|
||||||
target.setTitle(null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onPreExecute() {
|
|
||||||
bindView(null, mTarget);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected UidDetail doInBackground(Void... params) {
|
|
||||||
return mProvider.getUidDetail(mItem.key, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onPostExecute(UidDetail result) {
|
|
||||||
bindView(result, mTarget);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,86 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2017 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.android.settings.datausage;
|
||||||
|
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import android.graphics.Color;
|
||||||
|
import android.graphics.drawable.ColorDrawable;
|
||||||
|
|
||||||
|
import com.android.settings.TestConfig;
|
||||||
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
|
import com.android.settings.testutils.shadow.ShadowThreadUtils;
|
||||||
|
import com.android.settingslib.AppItem;
|
||||||
|
import com.android.settingslib.net.UidDetail;
|
||||||
|
import com.android.settingslib.net.UidDetailProvider;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
|
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION,
|
||||||
|
shadows = ShadowThreadUtils.class)
|
||||||
|
public class AppDataUsagePreferenceTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private UidDetailProvider mUidDetailProvider;
|
||||||
|
private AppItem mAppItem;
|
||||||
|
private UidDetail mUidDetail;
|
||||||
|
|
||||||
|
private AppDataUsagePreference mPreference;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
mAppItem = new AppItem(123);
|
||||||
|
mUidDetail = new UidDetail();
|
||||||
|
mUidDetail.icon = new ColorDrawable(Color.BLUE);
|
||||||
|
mUidDetail.label = "title";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void createPref_hasCachedUidDetail_shouldSetAppInfo() {
|
||||||
|
when(mUidDetailProvider.getUidDetail(mAppItem.key, false /* blocking */))
|
||||||
|
.thenReturn(mUidDetail);
|
||||||
|
|
||||||
|
mPreference = new AppDataUsagePreference(RuntimeEnvironment.application, mAppItem,
|
||||||
|
50 /* percent */, mUidDetailProvider);
|
||||||
|
|
||||||
|
assertThat(mPreference.getTitle()).isEqualTo(mUidDetail.label);
|
||||||
|
assertThat(mPreference.getIcon()).isEqualTo(mUidDetail.icon);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void createPref_noCachedUidDetail_shouldSetAppInfo() {
|
||||||
|
when(mUidDetailProvider.getUidDetail(mAppItem.key, true /* blocking */))
|
||||||
|
.thenReturn(mUidDetail);
|
||||||
|
|
||||||
|
mPreference = new AppDataUsagePreference(RuntimeEnvironment.application, mAppItem,
|
||||||
|
50 /* percent */, mUidDetailProvider);
|
||||||
|
|
||||||
|
assertThat(mPreference.getTitle()).isEqualTo(mUidDetail.label);
|
||||||
|
assertThat(mPreference.getIcon()).isEqualTo(mUidDetail.icon);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user