Android Open Source - screen-notifications Base Accessibility Service






From Project

Back to project page screen-notifications.

License

The source code is released under:

GNU General Public License

If you think the Android project screen-notifications listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2012 Luke Korth <korth.luke@gmail.com>
 * /*ww w . j av a2  s.  c o m*/
 * This file is part of Screen Notifications.
 * 
 * Screen Notifications is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Screen Notifications is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Screen Notifications.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.lukekorth.screennotifications;

import android.accessibilityservice.AccessibilityService;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.preference.PreferenceManager;
import android.view.accessibility.AccessibilityEvent;

public class BaseAccessibilityService extends AccessibilityService implements SensorEventListener {

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED &&
                isAppEnabled(event)) {
            if (isProximitySensorEnabled()) {
                if (!registerProximitySensorListener()) {
                    new ScreenController(this, false).handleNotification();
                }
            } else {
                new ScreenController(this, false).handleNotification();
            }
        }
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
            unregisterProximitySensorListener();

            boolean close = event.values[0] < event.sensor.getMaximumRange();
            new ScreenController(this, close).handleNotification();
        }
    }

    private boolean isAppEnabled(AccessibilityEvent event) {
        return PreferenceManager.getDefaultSharedPreferences(this)
                .getBoolean(event.getPackageName().toString(), false);
    }

    private boolean isProximitySensorEnabled() {
        return !PreferenceManager.getDefaultSharedPreferences(this).getBoolean("proxSensor", true);
    }

    private boolean registerProximitySensorListener() {
        SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        Sensor proximitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);

        if (proximitySensor == null) {
            return false;
        } else {
            sensorManager.registerListener(this, proximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
            return true;
        }
    }

    private void unregisterProximitySensorListener() {
        ((SensorManager) getSystemService(Context.SENSOR_SERVICE)).unregisterListener(this);
    }

    @Override
    public void onInterrupt() {}

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {}

}




Java Source Code List

com.lukekorth.screennotifications.AppAdapter.java
com.lukekorth.screennotifications.AppsActivity.java
com.lukekorth.screennotifications.BaseAccessibilityService.java
com.lukekorth.screennotifications.NotificationListener.java
com.lukekorth.screennotifications.ScreenController.java
com.lukekorth.screennotifications.ScreenNotificationsActivity.java
com.lukekorth.screennotifications.ScreenNotificationsServiceJB.java
com.lukekorth.screennotifications.ScreenNotificationsService.java
com.lukekorth.screennotifications.TimePreference.java
com.lukekorth.screennotifications.billing.Base64DecoderException.java
com.lukekorth.screennotifications.billing.Base64.java
com.lukekorth.screennotifications.billing.IabException.java
com.lukekorth.screennotifications.billing.IabHelper.java
com.lukekorth.screennotifications.billing.IabResult.java
com.lukekorth.screennotifications.billing.Inventory.java
com.lukekorth.screennotifications.billing.Purchase.java
com.lukekorth.screennotifications.billing.Security.java
com.lukekorth.screennotifications.billing.SkuDetails.java