• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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 
17 package com.android.settings;
18 
19 import org.xmlpull.v1.XmlPullParser;
20 import org.xmlpull.v1.XmlPullParserException;
21 
22 import android.app.Activity;
23 import android.app.ListFragment;
24 import android.app.admin.DeviceAdminInfo;
25 import android.app.admin.DeviceAdminReceiver;
26 import android.app.admin.DevicePolicyManager;
27 import android.content.ComponentName;
28 import android.content.Context;
29 import android.content.Intent;
30 import android.content.pm.PackageManager;
31 import android.content.pm.ResolveInfo;
32 import android.content.res.Resources;
33 import android.os.Bundle;
34 import android.util.Log;
35 import android.view.LayoutInflater;
36 import android.view.View;
37 import android.view.ViewGroup;
38 import android.widget.BaseAdapter;
39 import android.widget.CheckBox;
40 import android.widget.ImageView;
41 import android.widget.ListView;
42 import android.widget.TextView;
43 
44 import java.io.IOException;
45 import java.util.ArrayList;
46 import java.util.HashSet;
47 import java.util.List;
48 
49 public class DeviceAdminSettings extends ListFragment {
50     static final String TAG = "DeviceAdminSettings";
51 
52     static final int DIALOG_WARNING = 1;
53 
54     DevicePolicyManager mDPM;
55     final HashSet<ComponentName> mActiveAdmins = new HashSet<ComponentName>();
56     final ArrayList<DeviceAdminInfo> mAvailableAdmins = new ArrayList<DeviceAdminInfo>();
57 
58     @Override
onCreate(Bundle icicle)59     public void onCreate(Bundle icicle) {
60         super.onCreate(icicle);
61     }
62 
63     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)64     public View onCreateView(LayoutInflater inflater, ViewGroup container,
65             Bundle savedInstanceState) {
66         mDPM = (DevicePolicyManager) getActivity().getSystemService(Context.DEVICE_POLICY_SERVICE);
67         return inflater.inflate(R.layout.device_admin_settings, container, false);
68     }
69 
70     @Override
onResume()71     public void onResume() {
72         super.onResume();
73         updateList();
74     }
75 
updateList()76     void updateList() {
77         mActiveAdmins.clear();
78         List<ComponentName> cur = mDPM.getActiveAdmins();
79         if (cur != null) {
80             for (int i=0; i<cur.size(); i++) {
81                 mActiveAdmins.add(cur.get(i));
82             }
83         }
84 
85         mAvailableAdmins.clear();
86         List<ResolveInfo> avail = getActivity().getPackageManager().queryBroadcastReceivers(
87                 new Intent(DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED),
88                 PackageManager.GET_META_DATA);
89         int count = avail == null ? 0 : avail.size();
90         for (int i=0; i<count; i++) {
91             ResolveInfo ri = avail.get(i);
92             try {
93                 DeviceAdminInfo dpi = new DeviceAdminInfo(getActivity(), ri);
94                 if (dpi.isVisible() || mActiveAdmins.contains(dpi.getComponent())) {
95                     mAvailableAdmins.add(dpi);
96                 }
97             } catch (XmlPullParserException e) {
98                 Log.w(TAG, "Skipping " + ri.activityInfo, e);
99             } catch (IOException e) {
100                 Log.w(TAG, "Skipping " + ri.activityInfo, e);
101             }
102         }
103 
104         getListView().setAdapter(new PolicyListAdapter());
105     }
106 
107     @Override
onListItemClick(ListView l, View v, int position, long id)108     public void onListItemClick(ListView l, View v, int position, long id) {
109         DeviceAdminInfo dpi = (DeviceAdminInfo)l.getAdapter().getItem(position);
110         Intent intent = new Intent();
111         intent.setClass(getActivity(), DeviceAdminAdd.class);
112         intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, dpi.getComponent());
113         startActivity(intent);
114     }
115 
116     static class ViewHolder {
117         ImageView icon;
118         TextView name;
119         CheckBox checkbox;
120         TextView description;
121     }
122 
123     class PolicyListAdapter extends BaseAdapter {
124         final LayoutInflater mInflater;
125 
PolicyListAdapter()126         PolicyListAdapter() {
127             mInflater = (LayoutInflater)
128                     getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
129         }
130 
hasStableIds()131         public boolean hasStableIds() {
132             return true;
133         }
134 
getCount()135         public int getCount() {
136             return mAvailableAdmins.size();
137         }
138 
getItem(int position)139         public Object getItem(int position) {
140             return mAvailableAdmins.get(position);
141         }
142 
getItemId(int position)143         public long getItemId(int position) {
144             return position;
145         }
146 
areAllItemsEnabled()147         public boolean areAllItemsEnabled() {
148             return false;
149         }
150 
isEnabled(int position)151         public boolean isEnabled(int position) {
152             return true;
153         }
154 
getView(int position, View convertView, ViewGroup parent)155         public View getView(int position, View convertView, ViewGroup parent) {
156             View v;
157             if (convertView == null) {
158                 v = newView(parent);
159             } else {
160                 v = convertView;
161             }
162             bindView(v, position);
163             return v;
164         }
165 
newView(ViewGroup parent)166         public View newView(ViewGroup parent) {
167             View v = mInflater.inflate(R.layout.device_admin_item, parent, false);
168             ViewHolder h = new ViewHolder();
169             h.icon = (ImageView)v.findViewById(R.id.icon);
170             h.name = (TextView)v.findViewById(R.id.name);
171             h.checkbox = (CheckBox)v.findViewById(R.id.checkbox);
172             h.description = (TextView)v.findViewById(R.id.description);
173             v.setTag(h);
174             return v;
175         }
176 
bindView(View view, int position)177         public void bindView(View view, int position) {
178             final Activity activity = getActivity();
179             ViewHolder vh = (ViewHolder) view.getTag();
180             DeviceAdminInfo item = mAvailableAdmins.get(position);
181             vh.icon.setImageDrawable(item.loadIcon(activity.getPackageManager()));
182             vh.name.setText(item.loadLabel(activity.getPackageManager()));
183             vh.checkbox.setChecked(mActiveAdmins.contains(item.getComponent()));
184             try {
185                 vh.description.setText(item.loadDescription(activity.getPackageManager()));
186             } catch (Resources.NotFoundException e) {
187             }
188         }
189     }
190 }
191