New feature “Text and reading options” for SetupWizard, Wallpaper, and Settings (4/n).
- Extending the interface of AppGridView to be flexible. It will be used in the TextReadingPreferenceFragment. 1. Add a new attribute "appCounts" into the view. 2. Default app counts as 6 if not set the attribute. Bug: 211503117 Test: make -j64 RunSettingsRoboTests ROBOTEST_FILTER=AppGridViewTest Change-Id: I94a5f1d037551f6faba0300a176feb11fba1f203
This commit is contained in:
@@ -20,10 +20,12 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.UserHandle;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.IconDrawableFactory;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ArrayAdapter;
|
||||
@@ -31,6 +33,7 @@ import android.widget.GridView;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.core.util.Preconditions;
|
||||
|
||||
import com.android.settings.R;
|
||||
|
||||
@@ -38,7 +41,18 @@ import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The grid view for displaying the application entries.
|
||||
*
|
||||
* <p> The attribute value {@code appCount} from XML should be more than or equal to 1, otherwise
|
||||
* throws an {@link IllegalArgumentException}.</p>
|
||||
*/
|
||||
public class AppGridView extends GridView {
|
||||
private static final String TAG = "AppGridView";
|
||||
|
||||
private static final int APP_COUNT_DEF_VALUE = 6;
|
||||
private int mAppCount = APP_COUNT_DEF_VALUE;
|
||||
|
||||
public AppGridView(Context context) {
|
||||
super(context);
|
||||
init(context);
|
||||
@@ -46,24 +60,36 @@ public class AppGridView extends GridView {
|
||||
|
||||
public AppGridView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
applyAttributeSet(context, attrs);
|
||||
init(context);
|
||||
}
|
||||
|
||||
public AppGridView(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
applyAttributeSet(context, attrs);
|
||||
init(context);
|
||||
}
|
||||
|
||||
public AppGridView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleResId) {
|
||||
super(context, attrs, defStyleAttr, defStyleResId);
|
||||
|
||||
applyAttributeSet(context, attrs);
|
||||
init(context);
|
||||
|
||||
}
|
||||
|
||||
private void init(Context context) {
|
||||
setAdapter(new AppsAdapter(context, R.layout.screen_zoom_preview_app_icon,
|
||||
android.R.id.text1, android.R.id.icon1));
|
||||
android.R.id.text1, android.R.id.icon1, mAppCount));
|
||||
}
|
||||
|
||||
private void applyAttributeSet(Context context, AttributeSet attrs) {
|
||||
final TypedArray styledAttrs =
|
||||
context.obtainStyledAttributes(attrs, R.styleable.AppGridView);
|
||||
mAppCount =
|
||||
styledAttrs.getInteger(R.styleable.AppGridView_appCount, APP_COUNT_DEF_VALUE);
|
||||
Preconditions.checkArgument(mAppCount >= 1,
|
||||
/* errorMessage= */ "App count may not be negative or zero");
|
||||
|
||||
styledAttrs.recycle();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -73,12 +99,15 @@ public class AppGridView extends GridView {
|
||||
public static class AppsAdapter extends ArrayAdapter<ActivityEntry> {
|
||||
private final PackageManager mPackageManager;
|
||||
private final int mIconResId;
|
||||
private final int mAppCount;
|
||||
|
||||
public AppsAdapter(Context context, int layout, int textResId, int iconResId) {
|
||||
public AppsAdapter(Context context, int layout, int textResId, int iconResId,
|
||||
int appCount) {
|
||||
super(context, layout, textResId);
|
||||
|
||||
mIconResId = iconResId;
|
||||
mPackageManager = context.getPackageManager();
|
||||
mAppCount = appCount;
|
||||
|
||||
loadAllApps();
|
||||
}
|
||||
@@ -108,20 +137,24 @@ public class AppGridView extends GridView {
|
||||
}
|
||||
|
||||
private void loadAllApps() {
|
||||
final int needAppCount = 6;
|
||||
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
|
||||
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
|
||||
|
||||
final PackageManager pm = mPackageManager;
|
||||
final ArrayList<ActivityEntry> results = new ArrayList<>();
|
||||
final List<ResolveInfo> infos = pm.queryIntentActivities(mainIntent, 0);
|
||||
|
||||
if (mAppCount > infos.size()) {
|
||||
Log.d(TAG, "Visible app icon count does not meet the target count.");
|
||||
}
|
||||
|
||||
final IconDrawableFactory iconFactory = IconDrawableFactory.newInstance(getContext());
|
||||
for (ResolveInfo info : infos) {
|
||||
final CharSequence label = info.loadLabel(pm);
|
||||
if (label != null) {
|
||||
results.add(new ActivityEntry(info, label.toString(), iconFactory));
|
||||
}
|
||||
if (results.size() >= needAppCount) {
|
||||
if (results.size() >= mAppCount) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user