import android.annotation.LayoutRes;
import android.annotation.Nullable;
import android.app.Activity;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
import android.content.res.TypedArray;
+import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
+import android.util.Log;
import android.util.Pair;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.Toolbar;
import com.android.settingslib.R;
+import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class SettingsDrawerActivity extends Activity {
+ protected static final boolean DEBUG_TIMING = false;
+ private static final String TAG = "SettingsDrawerActivity";
+
+ private static List<DashboardCategory> sDashboardCategories;
+ private static HashMap<Pair<String, String>, DashboardTile> sTileCache;
+
+ private final PackageReceiver mPackageReceiver = new PackageReceiver();
+ private final List<CategoryListener> mCategoryListeners = new ArrayList<>();
+
private SettingsDrawerAdapter mDrawerAdapter;
- // Hold on to a cache of tiles to avoid loading the info multiple times.
- private final HashMap<Pair<String, String>, DashboardTile> mTileCache = new HashMap<>();
- private List<DashboardCategory> mDashboardCategories;
private DrawerLayout mDrawerLayout;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
+ long startTime = System.currentTimeMillis();
+
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.setContentView(R.layout.settings_with_drawer);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout = null;
return;
}
+ if (sDashboardCategories == null) {
+ sTileCache = new HashMap<>();
+ sDashboardCategories = TileUtils.getCategories(this, sTileCache);
+ }
setActionBar(toolbar);
mDrawerAdapter = new SettingsDrawerAdapter(this);
ListView listView = (ListView) findViewById(R.id.left_drawer);
onTileClicked(mDrawerAdapter.getTile(position));
};
});
+ if (DEBUG_TIMING) Log.d(TAG, "onCreate took " + (System.currentTimeMillis() - startTime)
+ + " ms");
}
@Override
protected void onResume() {
super.onResume();
- updateDrawer();
+ final IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
+ filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
+ filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
+ filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
+ filter.addDataScheme("package");
+ registerReceiver(mPackageReceiver, filter);
+
+ new CategoriesUpdater().execute();
+ }
+
+ @Override
+ protected void onPause() {
+ unregisterReceiver(mPackageReceiver);
+
+ super.onPause();
+ }
+
+ public void addCategoryListener(CategoryListener listener) {
+ mCategoryListeners.add(listener);
+ }
+
+ public void remCategoryListener(CategoryListener listener) {
+ mCategoryListeners.remove(listener);
}
public void openDrawer() {
}
}
- public List<DashboardCategory> getDashboardCategories(boolean force) {
- if (force) {
- mDashboardCategories = TileUtils.getCategories(this, mTileCache);
+ public List<DashboardCategory> getDashboardCategories() {
+ return sDashboardCategories;
+ }
+
+ protected void onCategoriesChanged() {
+ updateDrawer();
+ final int N = mCategoryListeners.size();
+ for (int i = 0; i < N; i++) {
+ mCategoryListeners.get(i).onCategoriesChanged();
}
- return mDashboardCategories;
}
public boolean openTile(DashboardTile tile) {
public void onProfileTileOpen() {
finish();
}
+
+ public interface CategoryListener {
+ void onCategoriesChanged();
+ }
+
+ private class CategoriesUpdater extends AsyncTask<Void, Void, List<DashboardCategory>> {
+ @Override
+ protected List<DashboardCategory> doInBackground(Void... params) {
+ return TileUtils.getCategories(SettingsDrawerActivity.this, sTileCache);
+ }
+
+ @Override
+ protected void onPostExecute(List<DashboardCategory> dashboardCategories) {
+ sDashboardCategories = dashboardCategories;
+ onCategoriesChanged();
+ }
+ }
+
+ private class PackageReceiver extends BroadcastReceiver {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ new CategoriesUpdater().execute();
+ }
+ }
}
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
+import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
public class TileUtils {
private static final boolean DEBUG = false;
+ private static final boolean DEBUG_TIMING = true;
private static final String LOG_TAG = "TileUtils";
private static final String SETTING_PKG = "com.android.settings";
- public static List<DashboardCategory> getCategories(Context context) {
- return getCategories(context, new HashMap<Pair<String, String>, DashboardTile>());
- }
-
public static List<DashboardCategory> getCategories(Context context,
HashMap<Pair<String, String>, DashboardTile> cache) {
+ final long startTime = System.currentTimeMillis();
ArrayList<DashboardTile> tiles = new ArrayList<>();
UserManager userManager = UserManager.get(context);
for (UserHandle user : userManager.getUserProfiles()) {
Collections.sort(category.tiles, TILE_COMPARATOR);
}
Collections.sort(categories, CATEGORY_COMPARATOR);
+ if (DEBUG_TIMING) Log.d(LOG_TAG, "getCategories took "
+ + (System.currentTimeMillis() - startTime) + " ms");
return categories;
}
activityInfo.packageName, activityInfo.name);
tile.category = categoryKey;
tile.priority = requireSettings ? resolved.priority : 0;
- updateTileData(context, tile);
+ tile.metaData = activityInfo.metaData;
+ updateTileData(context, tile, activityInfo, activityInfo.applicationInfo,
+ pm);
if (DEBUG) Log.d(LOG_TAG, "Adding tile " + tile.title);
addedCache.put(key, tile);
return null;
}
- private static boolean updateTileData(Context context, DashboardTile tile) {
- Intent intent = tile.intent;
- if (intent != null) {
- // Find the activity that is in the system image
- PackageManager pm = context.getPackageManager();
- List<ResolveInfo> list = tile.userHandle.size() != 0
- ? pm.queryIntentActivitiesAsUser(intent, PackageManager.GET_META_DATA,
- tile.userHandle.get(0).getIdentifier())
- : pm.queryIntentActivities(intent, PackageManager.GET_META_DATA);
- int listSize = list.size();
- for (int i = 0; i < listSize; i++) {
- ResolveInfo resolveInfo = list.get(i);
- if (resolveInfo.activityInfo.applicationInfo.isSystemApp()) {
- int icon = 0;
- CharSequence title = null;
- String summary = null;
-
- // Get the activity's meta-data
- try {
- Resources res = pm.getResourcesForApplication(
- resolveInfo.activityInfo.packageName);
- Bundle metaData = resolveInfo.activityInfo.metaData;
-
- if (res != null && metaData != null) {
- if (metaData.containsKey(META_DATA_PREFERENCE_ICON)) {
- icon = metaData.getInt(META_DATA_PREFERENCE_ICON);
- }
- if (metaData.containsKey(META_DATA_PREFERENCE_TITLE)) {
- title = metaData.getString(META_DATA_PREFERENCE_TITLE);
- }
- if (metaData.containsKey(META_DATA_PREFERENCE_SUMMARY)) {
- summary = metaData.getString(META_DATA_PREFERENCE_SUMMARY);
- }
- }
- } catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) {
- if (DEBUG) Log.d(LOG_TAG, "Couldn't find info", e);
+ private static boolean updateTileData(Context context, DashboardTile tile,
+ ActivityInfo activityInfo, ApplicationInfo applicationInfo, PackageManager pm) {
+ if (applicationInfo.isSystemApp()) {
+ int icon = 0;
+ CharSequence title = null;
+ String summary = null;
+
+ // Get the activity's meta-data
+ try {
+ Resources res = pm.getResourcesForApplication(
+ applicationInfo.packageName);
+ Bundle metaData = activityInfo.metaData;
+
+ if (res != null && metaData != null) {
+ if (metaData.containsKey(META_DATA_PREFERENCE_ICON)) {
+ icon = metaData.getInt(META_DATA_PREFERENCE_ICON);
}
-
- // Set the preference title to the activity's label if no
- // meta-data is found
- if (TextUtils.isEmpty(title)) {
- title = resolveInfo.loadLabel(pm).toString();
+ if (metaData.containsKey(META_DATA_PREFERENCE_TITLE)) {
+ title = metaData.getString(META_DATA_PREFERENCE_TITLE);
}
- if (icon == 0) {
- icon = resolveInfo.activityInfo.icon;
+ if (metaData.containsKey(META_DATA_PREFERENCE_SUMMARY)) {
+ summary = metaData.getString(META_DATA_PREFERENCE_SUMMARY);
}
-
- // Set icon, title and summary for the preference
- tile.icon = Icon.createWithResource(resolveInfo.activityInfo.packageName, icon);
- tile.title = title;
- tile.summary = summary;
- // Replace the intent with this specific activity
- tile.intent = new Intent().setClassName(resolveInfo.activityInfo.packageName,
- resolveInfo.activityInfo.name);
-
- return true;
}
+ } catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) {
+ if (DEBUG) Log.d(LOG_TAG, "Couldn't find info", e);
}
+
+ // Set the preference title to the activity's label if no
+ // meta-data is found
+ if (TextUtils.isEmpty(title)) {
+ title = activityInfo.loadLabel(pm).toString();
+ }
+ if (icon == 0) {
+ icon = activityInfo.icon;
+ }
+
+ // Set icon, title and summary for the preference
+ tile.icon = Icon.createWithResource(activityInfo.packageName, icon);
+ tile.title = title;
+ tile.summary = summary;
+ // Replace the intent with this specific activity
+ tile.intent = new Intent().setClassName(activityInfo.packageName,
+ activityInfo.name);
+
+ return true;
}
return false;