blob: bf750467d991d01d9deb5f8e742719b157b5c2cf [file] [log] [blame]
Jason Monk744b6362015-11-03 18:24:29 -05001/**
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;
Jason Monk6bea9502016-02-25 13:07:41 -050021import android.content.ActivityNotFoundException;
Jason Monke79790b2015-12-02 15:39:19 -050022import android.content.BroadcastReceiver;
23import android.content.Context;
24import android.content.Intent;
25import android.content.IntentFilter;
Jason Monkf18a55d2015-11-12 10:52:21 -050026import android.content.res.TypedArray;
Jason Monke79790b2015-12-02 15:39:19 -050027import android.os.AsyncTask;
Jason Monk744b6362015-11-03 18:24:29 -050028import android.os.Bundle;
Jason Monkcafda1f2016-02-05 10:16:22 -050029import android.provider.Settings;
Jason Monk744b6362015-11-03 18:24:29 -050030import android.support.v4.widget.DrawerLayout;
Jason Monke79790b2015-12-02 15:39:19 -050031import android.util.Log;
Jason Monk744b6362015-11-03 18:24:29 -050032import android.util.Pair;
33import android.view.Gravity;
34import android.view.LayoutInflater;
35import android.view.MenuItem;
36import android.view.View;
37import android.view.ViewGroup;
38import android.view.Window;
Jason Monkcafda1f2016-02-05 10:16:22 -050039import android.view.WindowManager.LayoutParams;
Jason Monk744b6362015-11-03 18:24:29 -050040import android.widget.AdapterView;
41import android.widget.ListView;
42import android.widget.Toolbar;
Jason Monk744b6362015-11-03 18:24:29 -050043import com.android.settingslib.R;
Jason Monkec02e472016-03-28 10:44:43 -040044import com.android.settingslib.applications.InterestingConfigChanges;
Jason Monk744b6362015-11-03 18:24:29 -050045
Jason Monke79790b2015-12-02 15:39:19 -050046import java.util.ArrayList;
Jason Monk744b6362015-11-03 18:24:29 -050047import java.util.HashMap;
48import java.util.List;
49
50public class SettingsDrawerActivity extends Activity {
51
Jason Monke79790b2015-12-02 15:39:19 -050052 protected static final boolean DEBUG_TIMING = false;
53 private static final String TAG = "SettingsDrawerActivity";
54
Clara Bayarri962694d2016-04-29 16:56:02 +010055 public static final String EXTRA_SHOW_MENU = "show_drawer_menu";
Jason Monk946483a2016-01-22 15:19:44 -050056
Jason Monke79790b2015-12-02 15:39:19 -050057 private static List<DashboardCategory> sDashboardCategories;
Jason Monkf509d7e2016-01-07 16:22:53 -050058 private static HashMap<Pair<String, String>, Tile> sTileCache;
Jason Monkec02e472016-03-28 10:44:43 -040059 private static InterestingConfigChanges sConfigTracker;
Jason Monke79790b2015-12-02 15:39:19 -050060
61 private final PackageReceiver mPackageReceiver = new PackageReceiver();
62 private final List<CategoryListener> mCategoryListeners = new ArrayList<>();
63
Jason Monk744b6362015-11-03 18:24:29 -050064 private SettingsDrawerAdapter mDrawerAdapter;
Jason Monk744b6362015-11-03 18:24:29 -050065 private DrawerLayout mDrawerLayout;
Jason Monk946483a2016-01-22 15:19:44 -050066 private boolean mShowingMenu;
Jason Monk744b6362015-11-03 18:24:29 -050067
68 @Override
69 protected void onCreate(@Nullable Bundle savedInstanceState) {
70 super.onCreate(savedInstanceState);
71
Jason Monke79790b2015-12-02 15:39:19 -050072 long startTime = System.currentTimeMillis();
73
Udam Saini48987f62016-03-03 13:20:18 -080074 TypedArray theme = getTheme().obtainStyledAttributes(android.R.styleable.Theme);
75 if (!theme.getBoolean(android.R.styleable.Theme_windowNoTitle, false)) {
76 getWindow().addFlags(LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
77 getWindow().addFlags(LayoutParams.FLAG_TRANSLUCENT_STATUS);
78 requestWindowFeature(Window.FEATURE_NO_TITLE);
79 }
Jason Monk744b6362015-11-03 18:24:29 -050080 super.setContentView(R.layout.settings_with_drawer);
81 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
Jason Monk51a94232015-11-04 14:57:15 -050082 if (mDrawerLayout == null) {
83 return;
84 }
Jason Monk744b6362015-11-03 18:24:29 -050085 Toolbar toolbar = (Toolbar) findViewById(R.id.action_bar);
Jason Monkf18a55d2015-11-12 10:52:21 -050086 if (theme.getBoolean(android.R.styleable.Theme_windowNoTitle, false)) {
87 toolbar.setVisibility(View.GONE);
88 mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
89 mDrawerLayout = null;
90 return;
91 }
Jason Monk2a5d79a2015-12-03 10:51:58 -050092 getDashboardCategories();
Jason Monk744b6362015-11-03 18:24:29 -050093 setActionBar(toolbar);
94 mDrawerAdapter = new SettingsDrawerAdapter(this);
95 ListView listView = (ListView) findViewById(R.id.left_drawer);
96 listView.setAdapter(mDrawerAdapter);
97 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
98 public void onItemClick(android.widget.AdapterView<?> parent, View view, int position,
99 long id) {
100 onTileClicked(mDrawerAdapter.getTile(position));
101 };
102 });
Jason Monke79790b2015-12-02 15:39:19 -0500103 if (DEBUG_TIMING) Log.d(TAG, "onCreate took " + (System.currentTimeMillis() - startTime)
104 + " ms");
Jason Monk744b6362015-11-03 18:24:29 -0500105 }
106
107 @Override
108 public boolean onOptionsItemSelected(MenuItem item) {
Jason Monk946483a2016-01-22 15:19:44 -0500109 if (mShowingMenu && mDrawerLayout != null && item.getItemId() == android.R.id.home
Jason Monk51a94232015-11-04 14:57:15 -0500110 && mDrawerAdapter.getCount() != 0) {
Jason Monk744b6362015-11-03 18:24:29 -0500111 openDrawer();
112 return true;
113 }
114 return super.onOptionsItemSelected(item);
115 }
116
117 @Override
118 protected void onResume() {
119 super.onResume();
120
Jason Monk2a5d79a2015-12-03 10:51:58 -0500121 if (mDrawerLayout != null) {
122 final IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
123 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
124 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
125 filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
126 filter.addDataScheme("package");
127 registerReceiver(mPackageReceiver, filter);
Jason Monke79790b2015-12-02 15:39:19 -0500128
Jason Monk2a5d79a2015-12-03 10:51:58 -0500129 new CategoriesUpdater().execute();
130 }
Jason Monk946483a2016-01-22 15:19:44 -0500131 if (getIntent() != null && getIntent().getBooleanExtra(EXTRA_SHOW_MENU, false)) {
132 showMenuIcon();
133 }
Jason Monke79790b2015-12-02 15:39:19 -0500134 }
135
136 @Override
137 protected void onPause() {
Jason Monk2a5d79a2015-12-03 10:51:58 -0500138 if (mDrawerLayout != null) {
139 unregisterReceiver(mPackageReceiver);
140 }
Jason Monke79790b2015-12-02 15:39:19 -0500141
142 super.onPause();
143 }
144
145 public void addCategoryListener(CategoryListener listener) {
146 mCategoryListeners.add(listener);
147 }
148
149 public void remCategoryListener(CategoryListener listener) {
150 mCategoryListeners.remove(listener);
Jason Monk744b6362015-11-03 18:24:29 -0500151 }
152
Anna Galusza1774a272016-02-01 15:44:00 -0800153 public void setIsDrawerPresent(boolean isPresent) {
Anna Galuszafb695c62016-02-02 10:06:05 -0800154 if (isPresent) {
Anna Galusza1774a272016-02-01 15:44:00 -0800155 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
156 updateDrawer();
157 } else {
Udam Saini68dab672016-03-29 10:49:18 -0700158 if (mDrawerLayout != null) {
159 mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
160 mDrawerLayout = null;
161 }
Anna Galusza1774a272016-02-01 15:44:00 -0800162 }
163 }
164
Jason Monk744b6362015-11-03 18:24:29 -0500165 public void openDrawer() {
Jason Monk51a94232015-11-04 14:57:15 -0500166 if (mDrawerLayout != null) {
167 mDrawerLayout.openDrawer(Gravity.START);
168 }
Jason Monk744b6362015-11-03 18:24:29 -0500169 }
170
171 public void closeDrawer() {
Jason Monk51a94232015-11-04 14:57:15 -0500172 if (mDrawerLayout != null) {
173 mDrawerLayout.closeDrawers();
174 }
Jason Monk744b6362015-11-03 18:24:29 -0500175 }
176
177 @Override
178 public void setContentView(@LayoutRes int layoutResID) {
Jason Monk4ea66572016-03-30 11:05:51 -0400179 final ViewGroup parent = (ViewGroup) findViewById(R.id.content_frame);
180 if (parent != null) {
181 parent.removeAllViews();
182 }
183 LayoutInflater.from(this).inflate(layoutResID, parent);
Jason Monk744b6362015-11-03 18:24:29 -0500184 }
185
186 @Override
187 public void setContentView(View view) {
188 ((ViewGroup) findViewById(R.id.content_frame)).addView(view);
189 }
190
191 @Override
192 public void setContentView(View view, ViewGroup.LayoutParams params) {
193 ((ViewGroup) findViewById(R.id.content_frame)).addView(view, params);
194 }
195
196 public void updateDrawer() {
Jason Monk51a94232015-11-04 14:57:15 -0500197 if (mDrawerLayout == null) {
198 return;
199 }
Jason Monk744b6362015-11-03 18:24:29 -0500200 // TODO: Do this in the background with some loading.
201 mDrawerAdapter.updateCategories();
Jason Monk51a94232015-11-04 14:57:15 -0500202 if (mDrawerAdapter.getCount() != 0) {
203 mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
Jason Monk51a94232015-11-04 14:57:15 -0500204 } else {
205 mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
206 }
Jason Monk744b6362015-11-03 18:24:29 -0500207 }
208
Jason Monk946483a2016-01-22 15:19:44 -0500209 public void showMenuIcon() {
210 mShowingMenu = true;
211 getActionBar().setHomeAsUpIndicator(R.drawable.ic_menu);
212 getActionBar().setDisplayHomeAsUpEnabled(true);
213 }
214
Jason Monke79790b2015-12-02 15:39:19 -0500215 public List<DashboardCategory> getDashboardCategories() {
Jason Monk2a5d79a2015-12-03 10:51:58 -0500216 if (sDashboardCategories == null) {
217 sTileCache = new HashMap<>();
Jason Monkec02e472016-03-28 10:44:43 -0400218 sConfigTracker = new InterestingConfigChanges();
Jason Monkb0ae8f82016-04-12 10:30:43 -0400219 // Apply initial current config.
220 sConfigTracker.applyNewConfig(getResources());
Jason Monk2a5d79a2015-12-03 10:51:58 -0500221 sDashboardCategories = TileUtils.getCategories(this, sTileCache);
222 }
Jason Monke79790b2015-12-02 15:39:19 -0500223 return sDashboardCategories;
224 }
225
226 protected void onCategoriesChanged() {
227 updateDrawer();
228 final int N = mCategoryListeners.size();
229 for (int i = 0; i < N; i++) {
230 mCategoryListeners.get(i).onCategoriesChanged();
Jason Monk744b6362015-11-03 18:24:29 -0500231 }
Jason Monk744b6362015-11-03 18:24:29 -0500232 }
233
Jason Monkf509d7e2016-01-07 16:22:53 -0500234 public boolean openTile(Tile tile) {
Jason Monk744b6362015-11-03 18:24:29 -0500235 closeDrawer();
Jason Monk3175a6e2015-11-30 15:05:35 -0500236 if (tile == null) {
Jason Monkcafda1f2016-02-05 10:16:22 -0500237 startActivity(new Intent(Settings.ACTION_SETTINGS).addFlags(
238 Intent.FLAG_ACTIVITY_CLEAR_TASK));
239 return true;
Jason Monk3175a6e2015-11-30 15:05:35 -0500240 }
Jason Monk6bea9502016-02-25 13:07:41 -0500241 try {
242 int numUserHandles = tile.userHandle.size();
243 if (numUserHandles > 1) {
244 ProfileSelectDialog.show(getFragmentManager(), tile);
245 return false;
246 } else if (numUserHandles == 1) {
247 // Show menu on top level items.
248 tile.intent.putExtra(EXTRA_SHOW_MENU, true);
249 tile.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
250 startActivityAsUser(tile.intent, tile.userHandle.get(0));
251 } else {
252 // Show menu on top level items.
253 tile.intent.putExtra(EXTRA_SHOW_MENU, true);
254 tile.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
255 startActivity(tile.intent);
256 }
257 } catch (ActivityNotFoundException e) {
258 Log.w(TAG, "Couldn't find tile " + tile.intent, e);
Jason Monk0d72d202015-11-04 13:16:00 -0500259 }
260 return true;
Jason Monk744b6362015-11-03 18:24:29 -0500261 }
262
Jason Monkf509d7e2016-01-07 16:22:53 -0500263 protected void onTileClicked(Tile tile) {
Jason Monk0d72d202015-11-04 13:16:00 -0500264 if (openTile(tile)) {
265 finish();
266 }
267 }
268
269 public void onProfileTileOpen() {
Jason Monk744b6362015-11-03 18:24:29 -0500270 finish();
271 }
Jason Monke79790b2015-12-02 15:39:19 -0500272
273 public interface CategoryListener {
274 void onCategoriesChanged();
275 }
276
277 private class CategoriesUpdater extends AsyncTask<Void, Void, List<DashboardCategory>> {
278 @Override
279 protected List<DashboardCategory> doInBackground(Void... params) {
Jason Monkec02e472016-03-28 10:44:43 -0400280 if (sConfigTracker.applyNewConfig(getResources())) {
281 sTileCache.clear();
282 }
Jason Monke79790b2015-12-02 15:39:19 -0500283 return TileUtils.getCategories(SettingsDrawerActivity.this, sTileCache);
284 }
285
286 @Override
287 protected void onPostExecute(List<DashboardCategory> dashboardCategories) {
288 sDashboardCategories = dashboardCategories;
289 onCategoriesChanged();
290 }
291 }
292
293 private class PackageReceiver extends BroadcastReceiver {
294 @Override
295 public void onReceive(Context context, Intent intent) {
296 new CategoriesUpdater().execute();
297 }
298 }
Jason Monk744b6362015-11-03 18:24:29 -0500299}