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