SettingsDrawerActivity.java revision 2a5d79aa6fd882ef6f46439f5156c516f173f06a
1/** 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package com.android.settingslib.drawer; 17 18import android.annotation.LayoutRes; 19import android.annotation.Nullable; 20import android.app.Activity; 21import android.content.BroadcastReceiver; 22import android.content.Context; 23import android.content.Intent; 24import android.content.IntentFilter; 25import android.content.res.TypedArray; 26import android.os.AsyncTask; 27import android.os.Bundle; 28import android.support.v4.widget.DrawerLayout; 29import android.util.Log; 30import android.util.Pair; 31import android.view.Gravity; 32import android.view.LayoutInflater; 33import android.view.MenuItem; 34import android.view.View; 35import android.view.ViewGroup; 36import android.view.Window; 37import android.widget.AdapterView; 38import android.widget.ListView; 39import android.widget.Toolbar; 40import com.android.settingslib.R; 41 42import java.util.ArrayList; 43import java.util.HashMap; 44import java.util.List; 45 46public class SettingsDrawerActivity extends Activity { 47 48 protected static final boolean DEBUG_TIMING = false; 49 private static final String TAG = "SettingsDrawerActivity"; 50 51 private static List<DashboardCategory> sDashboardCategories; 52 private static HashMap<Pair<String, String>, DashboardTile> sTileCache; 53 54 private final PackageReceiver mPackageReceiver = new PackageReceiver(); 55 private final List<CategoryListener> mCategoryListeners = new ArrayList<>(); 56 57 private SettingsDrawerAdapter mDrawerAdapter; 58 private DrawerLayout mDrawerLayout; 59 60 @Override 61 protected void onCreate(@Nullable Bundle savedInstanceState) { 62 super.onCreate(savedInstanceState); 63 64 long startTime = System.currentTimeMillis(); 65 66 requestWindowFeature(Window.FEATURE_NO_TITLE); 67 super.setContentView(R.layout.settings_with_drawer); 68 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 69 if (mDrawerLayout == null) { 70 return; 71 } 72 Toolbar toolbar = (Toolbar) findViewById(R.id.action_bar); 73 TypedArray theme = getTheme().obtainStyledAttributes(android.R.styleable.Theme); 74 if (theme.getBoolean(android.R.styleable.Theme_windowNoTitle, false)) { 75 toolbar.setVisibility(View.GONE); 76 mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); 77 mDrawerLayout = null; 78 return; 79 } 80 getDashboardCategories(); 81 setActionBar(toolbar); 82 mDrawerAdapter = new SettingsDrawerAdapter(this); 83 ListView listView = (ListView) findViewById(R.id.left_drawer); 84 listView.setAdapter(mDrawerAdapter); 85 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 86 public void onItemClick(android.widget.AdapterView<?> parent, View view, int position, 87 long id) { 88 onTileClicked(mDrawerAdapter.getTile(position)); 89 }; 90 }); 91 if (DEBUG_TIMING) Log.d(TAG, "onCreate took " + (System.currentTimeMillis() - startTime) 92 + " ms"); 93 } 94 95 @Override 96 public boolean onOptionsItemSelected(MenuItem item) { 97 if (mDrawerLayout != null && item.getItemId() == android.R.id.home 98 && mDrawerAdapter.getCount() != 0) { 99 openDrawer(); 100 return true; 101 } 102 return super.onOptionsItemSelected(item); 103 } 104 105 @Override 106 protected void onResume() { 107 super.onResume(); 108 109 if (mDrawerLayout != null) { 110 final IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED); 111 filter.addAction(Intent.ACTION_PACKAGE_REMOVED); 112 filter.addAction(Intent.ACTION_PACKAGE_CHANGED); 113 filter.addAction(Intent.ACTION_PACKAGE_REPLACED); 114 filter.addDataScheme("package"); 115 registerReceiver(mPackageReceiver, filter); 116 117 new CategoriesUpdater().execute(); 118 } 119 } 120 121 @Override 122 protected void onPause() { 123 if (mDrawerLayout != null) { 124 unregisterReceiver(mPackageReceiver); 125 } 126 127 super.onPause(); 128 } 129 130 public void addCategoryListener(CategoryListener listener) { 131 mCategoryListeners.add(listener); 132 } 133 134 public void remCategoryListener(CategoryListener listener) { 135 mCategoryListeners.remove(listener); 136 } 137 138 public void openDrawer() { 139 if (mDrawerLayout != null) { 140 mDrawerLayout.openDrawer(Gravity.START); 141 } 142 } 143 144 public void closeDrawer() { 145 if (mDrawerLayout != null) { 146 mDrawerLayout.closeDrawers(); 147 } 148 } 149 150 @Override 151 public void setContentView(@LayoutRes int layoutResID) { 152 LayoutInflater.from(this).inflate(layoutResID, 153 (ViewGroup) findViewById(R.id.content_frame)); 154 } 155 156 @Override 157 public void setContentView(View view) { 158 ((ViewGroup) findViewById(R.id.content_frame)).addView(view); 159 } 160 161 @Override 162 public void setContentView(View view, ViewGroup.LayoutParams params) { 163 ((ViewGroup) findViewById(R.id.content_frame)).addView(view, params); 164 } 165 166 public void updateDrawer() { 167 if (mDrawerLayout == null) { 168 return; 169 } 170 // TODO: Do this in the background with some loading. 171 mDrawerAdapter.updateCategories(); 172 if (mDrawerAdapter.getCount() != 0) { 173 mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); 174 getActionBar().setHomeAsUpIndicator(R.drawable.ic_menu); 175 getActionBar().setDisplayHomeAsUpEnabled(true); 176 } else { 177 mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); 178 } 179 } 180 181 public List<DashboardCategory> getDashboardCategories() { 182 if (sDashboardCategories == null) { 183 sTileCache = new HashMap<>(); 184 sDashboardCategories = TileUtils.getCategories(this, sTileCache); 185 } 186 return sDashboardCategories; 187 } 188 189 protected void onCategoriesChanged() { 190 updateDrawer(); 191 final int N = mCategoryListeners.size(); 192 for (int i = 0; i < N; i++) { 193 mCategoryListeners.get(i).onCategoriesChanged(); 194 } 195 } 196 197 public boolean openTile(DashboardTile tile) { 198 closeDrawer(); 199 if (tile == null) { 200 return false; 201 } 202 int numUserHandles = tile.userHandle.size(); 203 if (numUserHandles > 1) { 204 ProfileSelectDialog.show(getFragmentManager(), tile); 205 return false; 206 } else if (numUserHandles == 1) { 207 startActivityAsUser(tile.intent, tile.userHandle.get(0)); 208 } else { 209 startActivity(tile.intent); 210 } 211 return true; 212 } 213 214 protected void onTileClicked(DashboardTile tile) { 215 if (openTile(tile)) { 216 finish(); 217 } 218 } 219 220 public void onProfileTileOpen() { 221 finish(); 222 } 223 224 public interface CategoryListener { 225 void onCategoriesChanged(); 226 } 227 228 private class CategoriesUpdater extends AsyncTask<Void, Void, List<DashboardCategory>> { 229 @Override 230 protected List<DashboardCategory> doInBackground(Void... params) { 231 return TileUtils.getCategories(SettingsDrawerActivity.this, sTileCache); 232 } 233 234 @Override 235 protected void onPostExecute(List<DashboardCategory> dashboardCategories) { 236 sDashboardCategories = dashboardCategories; 237 onCategoriesChanged(); 238 } 239 } 240 241 private class PackageReceiver extends BroadcastReceiver { 242 @Override 243 public void onReceive(Context context, Intent intent) { 244 new CategoriesUpdater().execute(); 245 } 246 } 247} 248