blob: 64910fdf86956298d7a3a371f95f9de84844b3c1 [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
Jason Monk946483a2016-01-22 15:19:44 -050055 static final String EXTRA_SHOW_MENU = "show_drawer_menu";
56
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 {
158 mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
159 mDrawerLayout = null;
160 }
161 }
162
Jason Monk744b6362015-11-03 18:24:29 -0500163 public void openDrawer() {
Jason Monk51a94232015-11-04 14:57:15 -0500164 if (mDrawerLayout != null) {
165 mDrawerLayout.openDrawer(Gravity.START);
166 }
Jason Monk744b6362015-11-03 18:24:29 -0500167 }
168
169 public void closeDrawer() {
Jason Monk51a94232015-11-04 14:57:15 -0500170 if (mDrawerLayout != null) {
171 mDrawerLayout.closeDrawers();
172 }
Jason Monk744b6362015-11-03 18:24:29 -0500173 }
174
175 @Override
176 public void setContentView(@LayoutRes int layoutResID) {
177 LayoutInflater.from(this).inflate(layoutResID,
178 (ViewGroup) findViewById(R.id.content_frame));
179 }
180
181 @Override
182 public void setContentView(View view) {
183 ((ViewGroup) findViewById(R.id.content_frame)).addView(view);
184 }
185
186 @Override
187 public void setContentView(View view, ViewGroup.LayoutParams params) {
188 ((ViewGroup) findViewById(R.id.content_frame)).addView(view, params);
189 }
190
191 public void updateDrawer() {
Jason Monk51a94232015-11-04 14:57:15 -0500192 if (mDrawerLayout == null) {
193 return;
194 }
Jason Monk744b6362015-11-03 18:24:29 -0500195 // TODO: Do this in the background with some loading.
196 mDrawerAdapter.updateCategories();
Jason Monk51a94232015-11-04 14:57:15 -0500197 if (mDrawerAdapter.getCount() != 0) {
198 mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
Jason Monk51a94232015-11-04 14:57:15 -0500199 } else {
200 mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
201 }
Jason Monk744b6362015-11-03 18:24:29 -0500202 }
203
Jason Monk946483a2016-01-22 15:19:44 -0500204 public void showMenuIcon() {
205 mShowingMenu = true;
206 getActionBar().setHomeAsUpIndicator(R.drawable.ic_menu);
207 getActionBar().setDisplayHomeAsUpEnabled(true);
208 }
209
Jason Monke79790b2015-12-02 15:39:19 -0500210 public List<DashboardCategory> getDashboardCategories() {
Jason Monk2a5d79a2015-12-03 10:51:58 -0500211 if (sDashboardCategories == null) {
212 sTileCache = new HashMap<>();
Jason Monkec02e472016-03-28 10:44:43 -0400213 sConfigTracker = new InterestingConfigChanges();
Jason Monk2a5d79a2015-12-03 10:51:58 -0500214 sDashboardCategories = TileUtils.getCategories(this, sTileCache);
215 }
Jason Monke79790b2015-12-02 15:39:19 -0500216 return sDashboardCategories;
217 }
218
219 protected void onCategoriesChanged() {
220 updateDrawer();
221 final int N = mCategoryListeners.size();
222 for (int i = 0; i < N; i++) {
223 mCategoryListeners.get(i).onCategoriesChanged();
Jason Monk744b6362015-11-03 18:24:29 -0500224 }
Jason Monk744b6362015-11-03 18:24:29 -0500225 }
226
Jason Monkf509d7e2016-01-07 16:22:53 -0500227 public boolean openTile(Tile tile) {
Jason Monk744b6362015-11-03 18:24:29 -0500228 closeDrawer();
Jason Monk3175a6e2015-11-30 15:05:35 -0500229 if (tile == null) {
Jason Monkcafda1f2016-02-05 10:16:22 -0500230 startActivity(new Intent(Settings.ACTION_SETTINGS).addFlags(
231 Intent.FLAG_ACTIVITY_CLEAR_TASK));
232 return true;
Jason Monk3175a6e2015-11-30 15:05:35 -0500233 }
Jason Monk6bea9502016-02-25 13:07:41 -0500234 try {
235 int numUserHandles = tile.userHandle.size();
236 if (numUserHandles > 1) {
237 ProfileSelectDialog.show(getFragmentManager(), tile);
238 return false;
239 } else if (numUserHandles == 1) {
240 // Show menu on top level items.
241 tile.intent.putExtra(EXTRA_SHOW_MENU, true);
242 tile.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
243 startActivityAsUser(tile.intent, tile.userHandle.get(0));
244 } else {
245 // Show menu on top level items.
246 tile.intent.putExtra(EXTRA_SHOW_MENU, true);
247 tile.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
248 startActivity(tile.intent);
249 }
250 } catch (ActivityNotFoundException e) {
251 Log.w(TAG, "Couldn't find tile " + tile.intent, e);
Jason Monk0d72d202015-11-04 13:16:00 -0500252 }
253 return true;
Jason Monk744b6362015-11-03 18:24:29 -0500254 }
255
Jason Monkf509d7e2016-01-07 16:22:53 -0500256 protected void onTileClicked(Tile tile) {
Jason Monk0d72d202015-11-04 13:16:00 -0500257 if (openTile(tile)) {
258 finish();
259 }
260 }
261
262 public void onProfileTileOpen() {
Jason Monk744b6362015-11-03 18:24:29 -0500263 finish();
264 }
Jason Monke79790b2015-12-02 15:39:19 -0500265
266 public interface CategoryListener {
267 void onCategoriesChanged();
268 }
269
270 private class CategoriesUpdater extends AsyncTask<Void, Void, List<DashboardCategory>> {
271 @Override
272 protected List<DashboardCategory> doInBackground(Void... params) {
Jason Monkec02e472016-03-28 10:44:43 -0400273 if (sConfigTracker.applyNewConfig(getResources())) {
274 sTileCache.clear();
275 }
Jason Monke79790b2015-12-02 15:39:19 -0500276 return TileUtils.getCategories(SettingsDrawerActivity.this, sTileCache);
277 }
278
279 @Override
280 protected void onPostExecute(List<DashboardCategory> dashboardCategories) {
281 sDashboardCategories = dashboardCategories;
282 onCategoriesChanged();
283 }
284 }
285
286 private class PackageReceiver extends BroadcastReceiver {
287 @Override
288 public void onReceive(Context context, Intent intent) {
289 new CategoriesUpdater().execute();
290 }
291 }
Jason Monk744b6362015-11-03 18:24:29 -0500292}