blob: 1c032fac00de84c5a62870cddbcf6a01df20a7e9 [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 Monke79790b2015-12-02 15:39:19 -050021import android.content.BroadcastReceiver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.IntentFilter;
Jason Monkf18a55d2015-11-12 10:52:21 -050025import android.content.res.TypedArray;
Jason Monke79790b2015-12-02 15:39:19 -050026import android.os.AsyncTask;
Jason Monk744b6362015-11-03 18:24:29 -050027import android.os.Bundle;
Jason Monkcafda1f2016-02-05 10:16:22 -050028import android.provider.Settings;
Jason Monk744b6362015-11-03 18:24:29 -050029import android.support.v4.widget.DrawerLayout;
Jason Monke79790b2015-12-02 15:39:19 -050030import android.util.Log;
Jason Monk744b6362015-11-03 18:24:29 -050031import android.util.Pair;
32import android.view.Gravity;
33import android.view.LayoutInflater;
34import android.view.MenuItem;
35import android.view.View;
36import android.view.ViewGroup;
37import android.view.Window;
Jason Monkcafda1f2016-02-05 10:16:22 -050038import android.view.WindowManager.LayoutParams;
Jason Monk744b6362015-11-03 18:24:29 -050039import android.widget.AdapterView;
40import android.widget.ListView;
41import android.widget.Toolbar;
Jason Monk744b6362015-11-03 18:24:29 -050042import com.android.settingslib.R;
43
Jason Monke79790b2015-12-02 15:39:19 -050044import java.util.ArrayList;
Jason Monk744b6362015-11-03 18:24:29 -050045import java.util.HashMap;
46import java.util.List;
47
48public class SettingsDrawerActivity extends Activity {
49
Jason Monke79790b2015-12-02 15:39:19 -050050 protected static final boolean DEBUG_TIMING = false;
51 private static final String TAG = "SettingsDrawerActivity";
52
Jason Monk946483a2016-01-22 15:19:44 -050053 static final String EXTRA_SHOW_MENU = "show_drawer_menu";
54
Jason Monke79790b2015-12-02 15:39:19 -050055 private static List<DashboardCategory> sDashboardCategories;
Jason Monkf509d7e2016-01-07 16:22:53 -050056 private static HashMap<Pair<String, String>, Tile> sTileCache;
Jason Monke79790b2015-12-02 15:39:19 -050057
58 private final PackageReceiver mPackageReceiver = new PackageReceiver();
59 private final List<CategoryListener> mCategoryListeners = new ArrayList<>();
60
Jason Monk744b6362015-11-03 18:24:29 -050061 private SettingsDrawerAdapter mDrawerAdapter;
Jason Monk744b6362015-11-03 18:24:29 -050062 private DrawerLayout mDrawerLayout;
Jason Monk946483a2016-01-22 15:19:44 -050063 private boolean mShowingMenu;
Jason Monk744b6362015-11-03 18:24:29 -050064
65 @Override
66 protected void onCreate(@Nullable Bundle savedInstanceState) {
67 super.onCreate(savedInstanceState);
68
Jason Monke79790b2015-12-02 15:39:19 -050069 long startTime = System.currentTimeMillis();
70
Jason Monkcafda1f2016-02-05 10:16:22 -050071 getWindow().addFlags(LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
72 getWindow().addFlags(LayoutParams.FLAG_TRANSLUCENT_STATUS);
Jason Monk744b6362015-11-03 18:24:29 -050073 requestWindowFeature(Window.FEATURE_NO_TITLE);
74 super.setContentView(R.layout.settings_with_drawer);
75 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
Jason Monk51a94232015-11-04 14:57:15 -050076 if (mDrawerLayout == null) {
77 return;
78 }
Jason Monk744b6362015-11-03 18:24:29 -050079 Toolbar toolbar = (Toolbar) findViewById(R.id.action_bar);
Jason Monkf18a55d2015-11-12 10:52:21 -050080 TypedArray theme = getTheme().obtainStyledAttributes(android.R.styleable.Theme);
81 if (theme.getBoolean(android.R.styleable.Theme_windowNoTitle, false)) {
82 toolbar.setVisibility(View.GONE);
83 mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
84 mDrawerLayout = null;
85 return;
86 }
Jason Monk2a5d79a2015-12-03 10:51:58 -050087 getDashboardCategories();
Jason Monk744b6362015-11-03 18:24:29 -050088 setActionBar(toolbar);
89 mDrawerAdapter = new SettingsDrawerAdapter(this);
90 ListView listView = (ListView) findViewById(R.id.left_drawer);
91 listView.setAdapter(mDrawerAdapter);
92 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
93 public void onItemClick(android.widget.AdapterView<?> parent, View view, int position,
94 long id) {
95 onTileClicked(mDrawerAdapter.getTile(position));
96 };
97 });
Jason Monke79790b2015-12-02 15:39:19 -050098 if (DEBUG_TIMING) Log.d(TAG, "onCreate took " + (System.currentTimeMillis() - startTime)
99 + " ms");
Jason Monk744b6362015-11-03 18:24:29 -0500100 }
101
102 @Override
Jason Monkcafda1f2016-02-05 10:16:22 -0500103 public void onBackPressed() {
104 if (mShowingMenu) {
105 // If we are showing the menu, then we are a top level activity and the back should
106 // kick back to settings home.
107 openTile(null);
108 }
109 super.onBackPressed();
110 }
111
112 @Override
Jason Monk744b6362015-11-03 18:24:29 -0500113 public boolean onOptionsItemSelected(MenuItem item) {
Jason Monk946483a2016-01-22 15:19:44 -0500114 if (mShowingMenu && mDrawerLayout != null && item.getItemId() == android.R.id.home
Jason Monk51a94232015-11-04 14:57:15 -0500115 && mDrawerAdapter.getCount() != 0) {
Jason Monk744b6362015-11-03 18:24:29 -0500116 openDrawer();
117 return true;
118 }
119 return super.onOptionsItemSelected(item);
120 }
121
122 @Override
123 protected void onResume() {
124 super.onResume();
125
Jason Monk2a5d79a2015-12-03 10:51:58 -0500126 if (mDrawerLayout != null) {
127 final IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
128 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
129 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
130 filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
131 filter.addDataScheme("package");
132 registerReceiver(mPackageReceiver, filter);
Jason Monke79790b2015-12-02 15:39:19 -0500133
Jason Monk2a5d79a2015-12-03 10:51:58 -0500134 new CategoriesUpdater().execute();
135 }
Jason Monk946483a2016-01-22 15:19:44 -0500136 if (getIntent() != null && getIntent().getBooleanExtra(EXTRA_SHOW_MENU, false)) {
137 showMenuIcon();
138 }
Jason Monke79790b2015-12-02 15:39:19 -0500139 }
140
141 @Override
142 protected void onPause() {
Jason Monk2a5d79a2015-12-03 10:51:58 -0500143 if (mDrawerLayout != null) {
144 unregisterReceiver(mPackageReceiver);
145 }
Jason Monke79790b2015-12-02 15:39:19 -0500146
147 super.onPause();
148 }
149
150 public void addCategoryListener(CategoryListener listener) {
151 mCategoryListeners.add(listener);
152 }
153
154 public void remCategoryListener(CategoryListener listener) {
155 mCategoryListeners.remove(listener);
Jason Monk744b6362015-11-03 18:24:29 -0500156 }
157
Anna Galusza1774a272016-02-01 15:44:00 -0800158 public void setIsDrawerPresent(boolean isPresent) {
Anna Galuszafb695c62016-02-02 10:06:05 -0800159 if (isPresent) {
Anna Galusza1774a272016-02-01 15:44:00 -0800160 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
161 updateDrawer();
162 } else {
163 mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
164 mDrawerLayout = null;
165 }
166 }
167
Jason Monk744b6362015-11-03 18:24:29 -0500168 public void openDrawer() {
Jason Monk51a94232015-11-04 14:57:15 -0500169 if (mDrawerLayout != null) {
170 mDrawerLayout.openDrawer(Gravity.START);
171 }
Jason Monk744b6362015-11-03 18:24:29 -0500172 }
173
174 public void closeDrawer() {
Jason Monk51a94232015-11-04 14:57:15 -0500175 if (mDrawerLayout != null) {
176 mDrawerLayout.closeDrawers();
177 }
Jason Monk744b6362015-11-03 18:24:29 -0500178 }
179
180 @Override
181 public void setContentView(@LayoutRes int layoutResID) {
182 LayoutInflater.from(this).inflate(layoutResID,
183 (ViewGroup) findViewById(R.id.content_frame));
184 }
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<>();
218 sDashboardCategories = TileUtils.getCategories(this, sTileCache);
219 }
Jason Monke79790b2015-12-02 15:39:19 -0500220 return sDashboardCategories;
221 }
222
223 protected void onCategoriesChanged() {
224 updateDrawer();
225 final int N = mCategoryListeners.size();
226 for (int i = 0; i < N; i++) {
227 mCategoryListeners.get(i).onCategoriesChanged();
Jason Monk744b6362015-11-03 18:24:29 -0500228 }
Jason Monk744b6362015-11-03 18:24:29 -0500229 }
230
Jason Monkf509d7e2016-01-07 16:22:53 -0500231 public boolean openTile(Tile tile) {
Jason Monk744b6362015-11-03 18:24:29 -0500232 closeDrawer();
Jason Monk3175a6e2015-11-30 15:05:35 -0500233 if (tile == null) {
Jason Monkcafda1f2016-02-05 10:16:22 -0500234 startActivity(new Intent(Settings.ACTION_SETTINGS).addFlags(
235 Intent.FLAG_ACTIVITY_CLEAR_TASK));
236 return true;
Jason Monk3175a6e2015-11-30 15:05:35 -0500237 }
Jason Monk0d72d202015-11-04 13:16:00 -0500238 int numUserHandles = tile.userHandle.size();
239 if (numUserHandles > 1) {
240 ProfileSelectDialog.show(getFragmentManager(), tile);
241 return false;
242 } else if (numUserHandles == 1) {
Jason Monk946483a2016-01-22 15:19:44 -0500243 // Show menu on top level items.
244 tile.intent.putExtra(EXTRA_SHOW_MENU, true);
Jason Monkcafda1f2016-02-05 10:16:22 -0500245 tile.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
Jason Monk0d72d202015-11-04 13:16:00 -0500246 startActivityAsUser(tile.intent, tile.userHandle.get(0));
247 } else {
Jason Monk946483a2016-01-22 15:19:44 -0500248 // Show menu on top level items.
249 tile.intent.putExtra(EXTRA_SHOW_MENU, true);
Jason Monkcafda1f2016-02-05 10:16:22 -0500250 tile.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
Jason Monk0d72d202015-11-04 13:16:00 -0500251 startActivity(tile.intent);
252 }
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) {
273 return TileUtils.getCategories(SettingsDrawerActivity.this, sTileCache);
274 }
275
276 @Override
277 protected void onPostExecute(List<DashboardCategory> dashboardCategories) {
278 sDashboardCategories = dashboardCategories;
279 onCategoriesChanged();
280 }
281 }
282
283 private class PackageReceiver extends BroadcastReceiver {
284 @Override
285 public void onReceive(Context context, Intent intent) {
286 new CategoriesUpdater().execute();
287 }
288 }
Jason Monk744b6362015-11-03 18:24:29 -0500289}