PageRenderTime 35ms CodeModel.GetById 21ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/accessibilityPreferences/src/com/marvin/preferences/AccessibilityServicesListActivity.java

http://eyes-free.googlecode.com/
Java | 146 lines | 84 code | 16 blank | 46 comment | 6 complexity | 653dc29ac41ad48463afcfd16401a9cc MD5 | raw file
  1/*
  2 * Copyright 2010 Google Inc.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5 * use this file except in compliance with the License. You may obtain a copy of
  6 * 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, WITHOUT
 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 13 * License for the specific language governing permissions and limitations under
 14 * the License.
 15 */
 16
 17package com.marvin.preferences;
 18
 19import android.app.AlertDialog;
 20import android.app.ListActivity;
 21import android.content.ActivityNotFoundException;
 22import android.content.DialogInterface;
 23import android.content.Intent;
 24import android.content.pm.PackageManager;
 25import android.content.pm.ResolveInfo;
 26import android.view.View;
 27import android.widget.ArrayAdapter;
 28import android.widget.ListView;
 29
 30import java.util.ArrayList;
 31import java.util.List;
 32
 33/**
 34 * The main activity for the Accessibility Settings Manager. This class
 35 * essentially acts as a launcher for the preference activities for
 36 * accessibility services installed on the device. An activity that wishes to be
 37 * listed here should declare an intent filter with both of the following.
 38 * action: android.intent.action.MAIN category:
 39 * android.accessibilityservice.SERVICE_SETTINGS
 40 *
 41 * @author caseyburkhardt@google.com (Casey Burkhardt)
 42 */
 43public class AccessibilityServicesListActivity extends ListActivity {
 44
 45    public static final String ACTIVITY_ACCESSIBILITY_SERVICE_SETTINGS =
 46            "android.accessibilityservice.SERVICE_SETTINGS";
 47
 48    private ArrayAdapter<AccessibilityServiceResolveInfo> mAppListAdapter;
 49
 50    private List<AccessibilityServiceResolveInfo> mAppList;
 51
 52    private PackageManager mPackageManager;
 53    
 54    private AccessibilityServicesListActivity self = this;
 55    
 56    @Override
 57    public void onResume() {
 58        super.onResume();
 59        mPackageManager = getPackageManager();
 60        mAppList = getCompatibleServicesList();
 61        mAppListAdapter = new ArrayAdapter<AccessibilityServiceResolveInfo>(
 62                this, R.layout.list_layout, R.id.list_item, mAppList);
 63        setListAdapter(mAppListAdapter);
 64    }
 65
 66    @Override
 67    protected void onListItemClick(ListView l, View v, int position, long id) {
 68        AccessibilityServiceResolveInfo info = mAppListAdapter.getItem((int) id);
 69        Intent launchIntent = new Intent();
 70        launchIntent.setFlags(
 71                Intent.FLAG_ACTIVITY_NEW_TASK + Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
 72        launchIntent.setClassName(
 73                info.mResolveInfo.activityInfo.packageName, info.mResolveInfo.activityInfo.name);
 74        try {
 75            startActivity(launchIntent);
 76        } catch (ActivityNotFoundException e) {
 77            // If for some reason the activity can't be resolved, display an
 78            // error.
 79            AlertDialog.Builder builder = new AlertDialog.Builder(this);
 80            builder.setMessage(getString(R.string.launch_failed_message)).setCancelable(true)
 81                    .setPositiveButton(
 82                            getString(R.string.ok_button), new DialogInterface.OnClickListener() {
 83                                public void onClick(DialogInterface dialog, int id) {
 84                                    dialog.dismiss();
 85                                }
 86                            }).create().show();
 87        }
 88    }
 89
 90    /**
 91     * Creates a List of ResolveInfo objects based on activities responding to
 92     * the following intent filter. action: android.intent.action.MAIN category:
 93     * android.accessibilityservice.SERVICE_SETTINGS
 94     *
 95     *@return an ArrayList containing ResolveInfo objects for each
 96     *         accessibility service that has defined a settings activity.
 97     */
 98    private List<AccessibilityServiceResolveInfo> getCompatibleServicesList() {
 99        Intent targetIntent = new Intent(Intent.ACTION_MAIN);
100        targetIntent.addCategory(ACTIVITY_ACCESSIBILITY_SERVICE_SETTINGS);
101        return convertResolveInfoList(
102                mPackageManager.queryIntentActivities(targetIntent, 0));
103    }
104
105    /**
106     * Converts a given list of ResolveInfo objects to AccessibilityServiceInfo
107     * objects
108     *
109     * @param list a list of ResolveInfo objects
110     * @return a list of AccessibilityServiceObjects
111     */
112    public List<AccessibilityServiceResolveInfo> convertResolveInfoList(
113            List<ResolveInfo> list) {
114        ArrayList<AccessibilityServiceResolveInfo> result = new ArrayList<AccessibilityServiceResolveInfo>();
115        for (ResolveInfo info : list) {
116            result.add(new AccessibilityServiceResolveInfo(info));
117        }
118        return result;
119    }
120
121    /**
122     * A simple wrapper of the ResolveInfo class used to override toString so our
123     * list items appear correctly when using an ArrayAdapter.
124     */
125    private class AccessibilityServiceResolveInfo {
126        private ResolveInfo mResolveInfo;
127
128        public AccessibilityServiceResolveInfo(ResolveInfo info) {
129            mResolveInfo = info;
130        }
131
132        @Override
133        public String toString() {
134            String result = mResolveInfo
135                    .loadLabel(self.getPackageManager()).toString();
136            if (result.length() == 0) {
137                if (mResolveInfo.activityInfo.labelRes != 0) {
138                    result = getString(mResolveInfo.activityInfo.labelRes);
139                } else {
140                    result = mResolveInfo.activityInfo.name.toString();
141                }
142            }
143            return String.format(self.getString(R.string.template_list_item_text), result);
144        }
145    }
146}