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

http://eyes-free.googlecode.com/ · Java · 111 lines · 70 code · 21 blank · 20 comment · 8 complexity · 718693bf81acbf3f93f34647029110bc 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.ActivityManagerNative;
  18. // IMPORTANT! This app must be built against the Android framework code
  19. // because the following two classes are NOT in the SDK.
  20. import com.android.internal.telephony.Phone;
  21. import com.android.internal.telephony.PhoneFactory;
  22. import android.accessibilityservice.AccessibilityService;
  23. import android.accessibilityservice.AccessibilityServiceInfo;
  24. import android.content.Context;
  25. import android.content.SharedPreferences;
  26. import android.content.res.Configuration;
  27. import android.os.Build;
  28. import android.preference.PreferenceManager;
  29. import android.telephony.ServiceState;
  30. import android.util.Log;
  31. import android.view.accessibility.AccessibilityEvent;
  32. /**
  33. * Accessibility service that enlarges the text.
  34. */
  35. public class TextEnlargerService extends AccessibilityService {
  36. private static final String LOG_TAG = "TextEnlargerService";
  37. private static final int NOTIFICATION_TIMEOUT_MILLIS = 80;
  38. private final Configuration mCurConfig = new Configuration();
  39. private SharedPreferences prefs;
  40. private static boolean isRunning = false;
  41. @Override
  42. public void onServiceConnected() {
  43. if (!phoneCheckPassed()){
  44. this.stopSelf();
  45. return;
  46. }
  47. prefs = PreferenceManager.getDefaultSharedPreferences(this);
  48. AccessibilityServiceInfo info = new AccessibilityServiceInfo();
  49. info.eventTypes = AccessibilityEvent.TYPES_ALL_MASK;
  50. info.feedbackType = AccessibilityServiceInfo.FEEDBACK_AUDIBLE;
  51. info.notificationTimeout = NOTIFICATION_TIMEOUT_MILLIS;
  52. info.flags = AccessibilityServiceInfo.DEFAULT;
  53. setServiceInfo(info);
  54. isRunning = true;
  55. }
  56. public static boolean isServiceInitialized() {
  57. return isRunning;
  58. }
  59. @Override
  60. public synchronized void onAccessibilityEvent(AccessibilityEvent event) {
  61. if (!phoneCheckPassed()){
  62. this.stopSelf();
  63. return;
  64. }
  65. if (event == null) {
  66. Log.e(LOG_TAG, "Received null accessibility event.");
  67. return;
  68. }
  69. Log.e(LOG_TAG, "About to enlarge font");
  70. try {
  71. if (prefs.getBoolean(event.getPackageName().toString(), true)) {
  72. mCurConfig.fontScale = Float.parseFloat(prefs.getString("text_enlargement_factor",
  73. "1.5"));
  74. Log.e(LOG_TAG, "Enlarging font to:" + mCurConfig.fontScale);
  75. Configuration currentConfig = ActivityManagerNative.getDefault().getConfiguration();
  76. if (currentConfig.fontScale != mCurConfig.fontScale) {
  77. ActivityManagerNative.getDefault().updateConfiguration(mCurConfig);
  78. }
  79. Log.e(LOG_TAG, "Font enlarged");
  80. } else {
  81. Log.e(LOG_TAG, "Skipping font enlargement for this app");
  82. }
  83. } catch (Exception e) {
  84. Log.e(LOG_TAG, "Font enlargement failed");
  85. }
  86. }
  87. @Override
  88. public void onInterrupt() {
  89. }
  90. }