PageRenderTime 45ms CodeModel.GetById 12ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

/talkback_preics/src/com/google/android/marvin/talkback/Utils.java

http://eyes-free.googlecode.com/
Java | 157 lines | 84 code | 20 blank | 53 comment | 17 complexity | 14a37b3ec475bd9953a0b7cb5e19caa0 MD5 | raw file
  1/*
  2 * Copyright (C) 2010 The Android Open Source Project
  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
 17package com.google.android.marvin.talkback;
 18
 19import android.app.ActivityManager;
 20import android.app.Service;
 21import android.content.Context;
 22import android.content.pm.PackageInfo;
 23import android.content.pm.PackageManager;
 24import android.content.pm.PackageManager.NameNotFoundException;
 25import android.util.Log;
 26import android.view.accessibility.AccessibilityEvent;
 27import android.widget.CompoundButton;
 28
 29import java.util.List;
 30
 31/**
 32 * This class contains utility methods.
 33 *
 34 * @author svetoslavganov@google.com (Svetoslav R. Ganov)
 35 */
 36public class Utils {
 37
 38    /** Tag for logging. */
 39    private static final String LOG_TAG = Utils.class.getSimpleName();
 40
 41    /** Invalid version code for a package. */
 42    public static final int INVALID_VERSION_CODE = -1;
 43    
 44    /** String constant. */
 45    private static final String SPACE = " ";
 46    
 47    // this is need as a workaround for the adding of CompoundButton state by
 48    // the framework
 49    private static String sValueChecked;
 50
 51    private static String sValueNotChecked;
 52    
 53
 54    /**
 55     * @return The name of the current {@link android.app.Activity} given
 56     *         through the current <code>context</code>.
 57     */
 58    public static String getCurrentActivityName(Context context) {
 59        ActivityManager activityManager = (ActivityManager) context
 60                .getSystemService(Service.ACTIVITY_SERVICE);
 61        List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(1);
 62        if (!tasks.isEmpty()) {
 63            return tasks.get(0).topActivity.getClassName();
 64        }
 65        return null;
 66    }
 67
 68    /**
 69     * @return The package version code or {@link #INVALID_VERSION_CODE}
 70     *         if the package does not exist.
 71     */
 72    public static int getVersionCode(Context context, String packageName) {
 73        PackageManager packageManager = context.getPackageManager();
 74        try {
 75            PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
 76            return packageInfo.versionCode;
 77        } catch (NameNotFoundException e) {
 78            Log.e(LOG_TAG, "Could not find package: " + packageName, e);
 79            return INVALID_VERSION_CODE;
 80        }
 81    }
 82
 83    /**
 84     * @return The package version name or <code>null</code> if the package
 85     *         does not exist.
 86     */
 87    public static String getVersionName(Context context, String packageName) {
 88        PackageManager packageManager = context.getPackageManager();
 89        try {
 90            PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
 91            return packageInfo.versionName;
 92        } catch (NameNotFoundException e) {
 93            Log.e(LOG_TAG, "Could not find package: " + packageName, e);
 94            return null;
 95        }
 96    }
 97    
 98    /**
 99     * This is a workaround for the addition of checked/not checked message
100     * generated by the framework which should actually happen the the
101     * accessibility service.
102     */
103    private static void ensureCompoundButtonWorkaround() {
104        if (sValueChecked == null || sValueNotChecked == null) {
105            Context context = TalkBackService.asContext();
106            sValueChecked = context.getString(R.string.value_checked);
107            sValueNotChecked = context.getString(R.string.value_not_checked);
108        }
109    }
110    
111    /**
112     * Gets the text of an <code>event</code> by concatenating the text members
113     * (regardless of their priority) using space as a delimiter.
114     * 
115     * @param context The context from which to load required resources.
116     * @param event The event.
117     * @return The event text.
118     */
119    public static StringBuilder getEventText(Context context, AccessibilityEvent event) {
120        ensureCompoundButtonWorkaround();
121
122        StringBuilder aggregator = new StringBuilder();
123        List<CharSequence> eventText = event.getText();
124        if (context == null) {
125            String s = "";
126        }
127        Class<?> eventClass = ClassLoadingManager.getInstance().loadOrGetCachedClass(context,
128                event.getClassName().toString(), event.getPackageName().toString());
129
130        // here we have a special case since the framework is adding
131        // the string for the state of a CompoundButton but we also get the
132        // isChecked attribute
133        int stateStringIndex = -1;
134        if (eventClass != null && CompoundButton.class.isAssignableFrom(eventClass)) {
135            for (int i = 0, count = eventText.size(); i < count; i++) {
136                CharSequence next = eventText.get(i);
137                if (sValueChecked.equals(next) || sValueNotChecked.equals(next)) {
138                    stateStringIndex = i;
139                    break;
140                }
141            }
142        }
143
144        for (int i = 0, count = eventText.size(); i < count; i++) {
145            if (i != stateStringIndex) {
146                aggregator.append(eventText.get(i));
147                aggregator.append(SPACE);
148            }
149        }
150
151        if (aggregator.length() > 0) {
152            aggregator.deleteCharAt(aggregator.length() - 1);
153        }
154
155        return aggregator;
156    }
157}