• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.graphics.drawable.Icon;
19 import android.view.LayoutInflater;
20 import android.view.View;
21 import android.view.ViewGroup;
22 import android.widget.BaseAdapter;
23 import android.widget.ImageView;
24 import android.widget.TextView;
25 
26 import com.android.settingslib.R;
27 
28 import java.util.ArrayList;
29 import java.util.List;
30 
31 public class SettingsDrawerAdapter extends BaseAdapter {
32 
33     private final ArrayList<Item> mItems = new ArrayList<>();
34     private final SettingsDrawerActivity mActivity;
35 
SettingsDrawerAdapter(SettingsDrawerActivity activity)36     public SettingsDrawerAdapter(SettingsDrawerActivity activity) {
37         mActivity = activity;
38     }
39 
updateCategories()40     void updateCategories() {
41         List<DashboardCategory> categories = mActivity.getDashboardCategories();
42         mItems.clear();
43         // Spacer.
44         mItems.add(null);
45         Item tile = new Item();
46         tile.label = mActivity.getString(R.string.home);
47         tile.icon = Icon.createWithResource(mActivity, R.drawable.home);
48         mItems.add(tile);
49         for (int i = 0; i < categories.size(); i++) {
50             Item category = new Item();
51             category.icon = null;
52             DashboardCategory dashboardCategory = categories.get(i);
53             category.label = dashboardCategory.title;
54             mItems.add(category);
55             for (int j = 0; j < dashboardCategory.tiles.size(); j++) {
56                 tile = new Item();
57                 Tile dashboardTile = dashboardCategory.tiles.get(j);
58                 tile.label = dashboardTile.title;
59                 tile.icon = dashboardTile.icon;
60                 tile.tile = dashboardTile;
61                 mItems.add(tile);
62             }
63         }
64         notifyDataSetChanged();
65     }
66 
getTile(int position)67     public Tile getTile(int position) {
68         return mItems.get(position) != null ? mItems.get(position).tile : null;
69     }
70 
71     @Override
getCount()72     public int getCount() {
73         return mItems.size();
74     }
75 
76     @Override
getItem(int position)77     public Object getItem(int position) {
78         return mItems.get(position);
79     }
80 
81     @Override
getItemId(int position)82     public long getItemId(int position) {
83         return position;
84     }
85 
86     @Override
isEnabled(int position)87     public boolean isEnabled(int position) {
88         return mItems.get(position) != null && mItems.get(position).icon != null;
89     }
90 
91     @Override
getView(int position, View convertView, ViewGroup parent)92     public View getView(int position, View convertView, ViewGroup parent) {
93         Item item = mItems.get(position);
94         if (item == null) {
95             if (convertView == null || convertView.getId() != R.id.spacer) {
96                 convertView = LayoutInflater.from(mActivity).inflate(R.layout.drawer_spacer,
97                         parent, false);
98             }
99             return convertView;
100         }
101         if (convertView != null && convertView.getId() == R.id.spacer) {
102             convertView = null;
103         }
104         boolean isTile = item.icon != null;
105         if (convertView == null || (isTile != (convertView.getId() == R.id.tile_item))) {
106             convertView = LayoutInflater.from(mActivity).inflate(isTile ? R.layout.drawer_item
107                             : R.layout.drawer_category,
108                     parent, false);
109         }
110         if (isTile) {
111             ((ImageView) convertView.findViewById(android.R.id.icon)).setImageIcon(item.icon);
112         }
113         ((TextView) convertView.findViewById(android.R.id.title)).setText(item.label);
114         return convertView;
115     }
116 
117     private static class Item {
118         public Icon icon;
119         public CharSequence label;
120         public Tile tile;
121     }
122 }
123