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