Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame^] | 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 | */ |
| 16 | package com.android.settingslib.drawer; |
| 17 | |
| 18 | import android.annotation.LayoutRes; |
| 19 | import android.annotation.Nullable; |
| 20 | import android.app.Activity; |
| 21 | import android.content.BroadcastReceiver; |
| 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
| 24 | import android.content.IntentFilter; |
| 25 | import android.os.Bundle; |
| 26 | import android.support.v4.widget.DrawerLayout; |
| 27 | import android.util.Pair; |
| 28 | import android.view.Gravity; |
| 29 | import android.view.LayoutInflater; |
| 30 | import android.view.MenuItem; |
| 31 | import android.view.View; |
| 32 | import android.view.ViewGroup; |
| 33 | import android.view.Window; |
| 34 | import android.widget.AdapterView; |
| 35 | import android.widget.ListView; |
| 36 | import android.widget.Toolbar; |
| 37 | |
| 38 | import com.android.settingslib.R; |
| 39 | |
| 40 | import java.util.HashMap; |
| 41 | import java.util.List; |
| 42 | |
| 43 | public class SettingsDrawerActivity extends Activity { |
| 44 | |
| 45 | private SettingsDrawerAdapter mDrawerAdapter; |
| 46 | // Hold on to a cache of tiles to avoid loading the info multiple times. |
| 47 | private final HashMap<Pair<String, String>, DashboardTile> mTileCache = new HashMap<>(); |
| 48 | private List<DashboardCategory> mDashboardCategories; |
| 49 | private DrawerLayout mDrawerLayout; |
| 50 | |
| 51 | @Override |
| 52 | protected void onCreate(@Nullable Bundle savedInstanceState) { |
| 53 | super.onCreate(savedInstanceState); |
| 54 | |
| 55 | requestWindowFeature(Window.FEATURE_NO_TITLE); |
| 56 | super.setContentView(R.layout.settings_with_drawer); |
| 57 | mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); |
| 58 | // Nope. |
| 59 | Toolbar toolbar = (Toolbar) findViewById(R.id.action_bar); |
| 60 | setActionBar(toolbar); |
| 61 | mDrawerAdapter = new SettingsDrawerAdapter(this); |
| 62 | ListView listView = (ListView) findViewById(R.id.left_drawer); |
| 63 | listView.setAdapter(mDrawerAdapter); |
| 64 | listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { |
| 65 | public void onItemClick(android.widget.AdapterView<?> parent, View view, int position, |
| 66 | long id) { |
| 67 | onTileClicked(mDrawerAdapter.getTile(position)); |
| 68 | }; |
| 69 | }); |
| 70 | getActionBar().setHomeAsUpIndicator(R.drawable.ic_menu); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public boolean onOptionsItemSelected(MenuItem item) { |
| 75 | if (item.getItemId() == android.R.id.home) { |
| 76 | openDrawer(); |
| 77 | return true; |
| 78 | } |
| 79 | return super.onOptionsItemSelected(item); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | protected void onResume() { |
| 84 | super.onResume(); |
| 85 | |
| 86 | mDrawerAdapter.updateCategories(); |
| 87 | } |
| 88 | |
| 89 | public void openDrawer() { |
| 90 | mDrawerLayout.openDrawer(Gravity.START); |
| 91 | } |
| 92 | |
| 93 | public void closeDrawer() { |
| 94 | mDrawerLayout.closeDrawers(); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void setContentView(@LayoutRes int layoutResID) { |
| 99 | LayoutInflater.from(this).inflate(layoutResID, |
| 100 | (ViewGroup) findViewById(R.id.content_frame)); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void setContentView(View view) { |
| 105 | ((ViewGroup) findViewById(R.id.content_frame)).addView(view); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public void setContentView(View view, ViewGroup.LayoutParams params) { |
| 110 | ((ViewGroup) findViewById(R.id.content_frame)).addView(view, params); |
| 111 | } |
| 112 | |
| 113 | public void updateDrawer() { |
| 114 | // TODO: Do this in the background with some loading. |
| 115 | mDrawerAdapter.updateCategories(); |
| 116 | getActionBar().setDisplayHomeAsUpEnabled(mDrawerAdapter.getCount() != 0); |
| 117 | } |
| 118 | |
| 119 | public List<DashboardCategory> getDashboardCategories(boolean force) { |
| 120 | if (force) { |
| 121 | mDashboardCategories = TileUtils.getCategories(this, mTileCache); |
| 122 | } |
| 123 | return mDashboardCategories; |
| 124 | } |
| 125 | |
| 126 | protected void openTile(DashboardTile tile) { |
| 127 | closeDrawer(); |
| 128 | startActivity(tile.intent); |
| 129 | } |
| 130 | |
| 131 | protected void onTileClicked(DashboardTile tile) { |
| 132 | openTile(tile); |
| 133 | finish(); |
| 134 | } |
| 135 | } |