PageRenderTime 17ms CodeModel.GetById 8ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 1ms

/TalkBack/src/com/google/android/marvin/talkback/BasePackageMonitor.java

http://eyes-free.googlecode.com/
Java | 129 lines | 53 code | 18 blank | 58 comment | 11 complexity | 1eefae54fbb85962919a1c72e6067b68 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.content.BroadcastReceiver;
 20import android.content.Context;
 21import android.content.Intent;
 22import android.content.IntentFilter;
 23import android.net.Uri;
 24
 25/**
 26 * Helper class for monitoring packages on the system.
 27 * 
 28 * @author svetoslavganov@google.com (Svetoslav R. Ganov)
 29 * @author alanv@google.com (Alan Viverette)
 30 */
 31abstract class BasePackageMonitor extends BroadcastReceiver {
 32
 33    /**
 34     * The intent filter to match package modifications.
 35     */
 36    private final IntentFilter mPackageFilter;
 37
 38    /**
 39     * The context in which this monitor is registered.
 40     */
 41    private Context mRegisteredContext;
 42
 43    /**
 44     * Creates a new instance.
 45     */
 46    public BasePackageMonitor() {
 47        mPackageFilter = new IntentFilter();
 48        mPackageFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
 49        mPackageFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
 50        mPackageFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
 51        mPackageFilter.addDataScheme("package");
 52    }
 53
 54    /**
 55     * Register this monitor via the given <code>context</code>. Throws an
 56     * {@link IllegalStateException} if this monitor was already registered.
 57     */
 58    public void register(Context context) {
 59        if (mRegisteredContext != null) {
 60            throw new IllegalStateException("Already registered");
 61        }
 62
 63        mRegisteredContext = context;
 64        context.registerReceiver(this, mPackageFilter);
 65    }
 66
 67    /**
 68     * Unregister this monitor. Throws an {@link IllegalStateException} if this
 69     * monitor wasn't registered.
 70     */
 71    public void unregister() {
 72        if (mRegisteredContext == null) {
 73            throw new IllegalStateException("Not registered");
 74        }
 75
 76        mRegisteredContext.unregisterReceiver(this);
 77        mRegisteredContext = null;
 78    }
 79
 80    @Override
 81    public void onReceive(Context context, Intent intent) {
 82        final String packageName = getPackageName(intent);
 83        final String action = intent.getAction();
 84
 85        if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
 86            onPackageAdded(packageName);
 87        } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
 88            onPackageRemoved(packageName);
 89        } else if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
 90            onPackageChanged(packageName);
 91        }
 92    }
 93
 94    /**
 95     * @return The name of the package from an <code>intent</code>.
 96     */
 97    private static String getPackageName(Intent intent) {
 98        final Uri uri = intent.getData();
 99
100        if (uri == null) {
101            return null;
102        }
103
104        return uri.getSchemeSpecificPart();
105    }
106
107    /**
108     * Called when a new application package has been installed on the device.
109     * 
110     * @param packageName The name of the package that was added.
111     */
112    protected abstract void onPackageAdded(String packageName);
113
114    /**
115     * Called when an existing application package has been removed from the
116     * device.
117     * 
118     * @param packageName The name of the package that was removed.
119     */
120    protected abstract void onPackageRemoved(String packageName);
121
122    /**
123     * Called when an existing application package has been changed (e.g. a
124     * component has been disabled or enabled).
125     * 
126     * @param packageName The name of the package that was changed.
127     */
128    protected abstract void onPackageChanged(String packageName);
129}