PageRenderTime 13ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

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