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

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

http://eyes-free.googlecode.com/
Java | 108 lines | 50 code | 13 blank | 45 comment | 10 complexity | d7c61e32b335c8e481e95bc1ed314b9c 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 */
 28public class BasePackageMonitor extends BroadcastReceiver {
 29
 30    /**
 31     * The intent filter to match package modifications.
 32     */
 33    final IntentFilter mPackageFilter = new IntentFilter();
 34
 35    /**
 36     * The context in which this monitor is registered.
 37     */
 38    private Context mRegisteredContext;
 39
 40    /**
 41     * Creates a new instance.
 42     */
 43    public BasePackageMonitor() {
 44        mPackageFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
 45        mPackageFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
 46        mPackageFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
 47        mPackageFilter.addDataScheme("package");
 48    }
 49
 50    /**
 51     * Register this monitor via the given <code>context</code>.
 52     */
 53    public void register(Context context) {
 54        if (mRegisteredContext != null) {
 55            throw new IllegalStateException("Already registered");
 56        }
 57        mRegisteredContext = context;
 58        context.registerReceiver(this, mPackageFilter);
 59    }
 60
 61    /**
 62     * Unregister this monitor.
 63     */
 64    public void unregister() {
 65        if (mRegisteredContext == null) {
 66            throw new IllegalStateException("Not registered");
 67        }
 68        mRegisteredContext.unregisterReceiver(this);
 69        mRegisteredContext = null;
 70    }
 71
 72    @Override
 73    public void onReceive(Context context, Intent intent) {
 74        String packageName = getPackageName(intent);
 75        String action = intent.getAction();
 76        if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
 77            onPackageAdded(packageName);
 78        } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
 79            onPackageRemoved(packageName);
 80        } else if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
 81            onPackageChanged(packageName);
 82        }
 83    }
 84
 85    /**
 86     * @return The name of the package from an <code>intent</code>.
 87     */
 88    private String getPackageName(Intent intent) {
 89        Uri uri = intent.getData();
 90        String packageName = uri != null ? uri.getSchemeSpecificPart() : null;
 91        return packageName;
 92    }
 93
 94    /**
 95     * Called when a <code>packageName</code> is added.
 96     */
 97    protected void onPackageAdded(String packageName) {}
 98
 99    /**
100     * Called when a <code>packageName</code> is removed.
101     */
102    protected void onPackageRemoved(String packageName) {}
103
104    /**
105     * Called when a <code>packageName</code> is changes.
106     */
107    protected void onPackageChanged(String packageName) {}
108}