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

/TextEnlarger/src/com/ideal/textenlarger/ApplicationsListActivity.java

http://eyes-free.googlecode.com/
Java | 102 lines | 58 code | 16 blank | 28 comment | 5 complexity | 2902acb3e8c3ee99967479ac9f11d0c5 MD5 | raw file
  1/*
  2 * Copyright (C) 2010 The IDEAL Group
  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 */
 16package com.ideal.textenlarger;
 17
 18
 19import android.app.ListActivity;
 20import android.content.SharedPreferences;
 21import android.content.SharedPreferences.Editor;
 22import android.content.pm.PackageInfo;
 23import android.content.pm.PackageManager;
 24import android.os.Bundle;
 25import android.preference.PreferenceManager;
 26import java.util.ArrayList;
 27import java.util.Collections;
 28import java.util.HashMap;
 29import java.util.List;
 30
 31/**
 32 * Presents the user with a list of all the apps they have installed.
 33 * If they uncheck an app, Text Enlarger will not run for that app.
 34 * This is needed to block Text Enlarger from running on incompatible apps.
 35 *
 36 * This was taken from a checkbox list tutorial at anddev.org:
 37 * http://www.anddev.org/extended_checkbox_list__extension_of_checkbox_text_list_tu-t5734.html
 38 */
 39public class ApplicationsListActivity extends ListActivity {
 40	
 41	private ExtendedCheckBoxListAdapter mListAdapter;
 42	
 43    // Create CheckBox List Adapter, cbla
 44    private String[] items = {
 45        "item"
 46    };
 47    
 48    private HashMap<String, String> labelToPackageName;
 49    private SharedPreferences prefs;
 50    private Editor prefsEditor;
 51	
 52    /** Called when the activity is first created. */
 53    @Override
 54    public void onCreate(Bundle savedInstanceState) {
 55        super.onCreate(savedInstanceState);
 56        
 57        prefs = PreferenceManager.getDefaultSharedPreferences(this);
 58        prefsEditor = prefs.edit();
 59        
 60        if (!prefs.contains("com.android.browser")){
 61            prefsEditor.putBoolean("com.android.browser", false);
 62            prefsEditor.commit();
 63        }
 64        
 65        labelToPackageName = new HashMap<String, String>();
 66        ArrayList<String> appsList = new ArrayList<String>();
 67        PackageManager pmPack;
 68        pmPack = getPackageManager();
 69        List<PackageInfo> packinfo = pmPack.getInstalledPackages(PackageManager.GET_ACTIVITIES);
 70        for (int i = 0; i < packinfo.size(); i++) {
 71            if ((packinfo.get(i).applicationInfo != null)) {
 72                String label = packinfo.get(i).applicationInfo.loadLabel(pmPack).toString();
 73                appsList.add(label);
 74                labelToPackageName.put(label, packinfo.get(i).packageName);
 75            }
 76        }
 77        Collections.sort(appsList);
 78        items = appsList.toArray(new String[]{});
 79        
 80        setContentView(R.layout.main);
 81        
 82        // Build the list adapter
 83        mListAdapter = new ExtendedCheckBoxListAdapter(this);
 84        
 85        // Add some items        
 86        for (int k = 0; k < items.length; k++) {
 87            String packageName = labelToPackageName.get(items[k]);
 88            boolean checked = prefs.getBoolean(packageName, true);
 89            mListAdapter.addItem(new ExtendedCheckBox(items[k], checked));
 90        }
 91        
 92        // Bind it to the activity!
 93        setListAdapter(mListAdapter);
 94    }
 95    
 96    public void setCheckedState(String label, boolean value){
 97        String packageName = labelToPackageName.get(label);
 98        prefsEditor.putBoolean(packageName, value);
 99        prefsEditor.commit();       
100    }
101    
102}