Android Open Source - cicada Notification Accessibility Service






From Project

Back to project page cicada.

License

The source code is released under:

Apache License

If you think the Android project cicada 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 (C) 2011 Cicada contributors
///*  w  w w. j  ava 2s.  com*/
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.package org.cicadasong.cicada;

package org.cicadasong.samples.notifications;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.app.Notification;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityEvent;
import android.widget.LinearLayout;
import android.widget.RemoteViews;
import android.widget.TextView;

public class NotificationAccessibilityService extends AccessibilityService {
  public static final String TAG = Notifications.TAG;

  @Override
  protected void onServiceConnected() {
    AccessibilityServiceInfo info = new AccessibilityServiceInfo();
    info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
    info.feedbackType = AccessibilityServiceInfo.FEEDBACK_VISUAL;
    setServiceInfo(info);

    super.onServiceConnected();
  }

  @Override
  public void onAccessibilityEvent(AccessibilityEvent event) {
    Log.v(TAG, "onAccessibilityEvent " + event);
    if (event.getEventType() != AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
      return;
    }
    
    if (event.getParcelableData() instanceof Notification) {
      Notification notification = (Notification) event.getParcelableData();
      String titleText = null;
      String bodyText = null;

      // This is an ugly hack to pull more interesting text out of the notification, but it often
      // works.
      RemoteViews contentView = notification.contentView;
      ViewGroup notificationView = (ViewGroup) contentView.apply(this, new LinearLayout(this));
      if (notificationView != null) {
        titleText = getTitleText(notificationView);
        bodyText = getBodyText(notificationView);
      }
      
      // We're just blasting this out on a broadcast intent, maybe we should find a more
      // discreet way.
      Intent intent = new Intent(Notifications.INTENT_NOTIFICATION);
      intent.putExtra(Notifications.EXTRA_TICKER_TEXT, notification.tickerText);
      intent.putExtra(Notifications.EXTRA_TITLE_TEXT, titleText);
      intent.putExtra(Notifications.EXTRA_BODY_TEXT, bodyText);
      intent.putExtra(Notifications.EXTRA_ICON_ID, notification.icon);
      intent.putExtra(Notifications.EXTRA_PACKAGE, event.getPackageName());
      sendBroadcast(intent);
    }
  }
  
  private String getTitleText(ViewGroup contentView) {
    return getText(contentView, new int[]{ 0, 1 });  // Empirically determined
  }
  
  private String getBodyText(ViewGroup contentView) {
    return getText(contentView, new int[]{ 1, 0 });
  }
 
  private String getText(ViewGroup contentView, int[] path) {
    View view = getView(contentView, path);
    if (!(view instanceof TextView)) {
      return null;
    }
    
    return ((TextView) view).getText().toString();
  }
  
  private View getView(ViewGroup root, int[] path) {
    ViewGroup current = root;
    for (int i = 0; i < path.length; i++) {
      if (current.getChildCount() <= path[i]) {
        return null;
      }
      View child = current.getChildAt(path[i]);
      if (i == path.length - 1) {
        return child;
      }
      if (!(child instanceof ViewGroup)) {
        return null;
      }
      current = (ViewGroup) child;
    }
    return null;
  }

  @Override
  public void onInterrupt() {
  }

}




Java Source Code List

com.hydraproductions.cicada.StopWatch.java
org.cicadasong.apollo.ApolloConfig.java
org.cicadasong.apollo.ApolloIntents.java
org.cicadasong.apollo.BitmapUtil.java
org.cicadasong.apollo.SimulatedDisplayView.java
org.cicadasong.cicada.AppDatabase.java
org.cicadasong.cicada.AppDescription.java
org.cicadasong.cicada.AppList.java
org.cicadasong.cicada.AppScanner.java
org.cicadasong.cicada.CicadaService.java
org.cicadasong.cicada.CicadaSettingsActivity.java
org.cicadasong.cicada.Cicada.java
org.cicadasong.cicada.DeviceService.java
org.cicadasong.cicada.HotkeySetupActivity.java
org.cicadasong.cicada.HotkeySetupEntry.java
org.cicadasong.cicada.MetaWatchConnection.java
org.cicadasong.cicada.NotificationRenderer.java
org.cicadasong.cicada.PackageMonitor.java
org.cicadasong.cicada.PackageUtil.java
org.cicadasong.cicada.PrefUtil.java
org.cicadasong.cicada.WidgetScreen.java
org.cicadasong.cicada.WidgetSetup.java
org.cicadasong.cicadalib.CicadaApp.java
org.cicadasong.cicadalib.CicadaIntents.java
org.cicadasong.cicadalib.CicadaNotificationManager.java
org.cicadasong.cicadalib.CicadaNotification.java
org.cicadasong.cicadalib.TextBlock.java
org.cicadasong.samples.bitly.Bitly.java
org.cicadasong.samples.digitalclock.DigitalClock.java
org.cicadasong.samples.hellocicada.HelloCicada.java
org.cicadasong.samples.nextbuses.NextBuses.java
org.cicadasong.samples.notificationdemo.NotificationDemo.java
org.cicadasong.samples.notifications.NotificationAccessibilityService.java
org.cicadasong.samples.notifications.Notifications.java
org.cicadasong.samples.quicktext.QuickTextSetup.java
org.cicadasong.samples.quicktext.QuickText.java
org.cicadasong.samples.smsnotifier.SMSBroadcastReceiver.java
org.cicadasong.samples.tubestatus.Preferences.java
org.cicadasong.samples.tubestatus.TubeStatusSettingsActivity.java
org.cicadasong.samples.tubestatus.TubeStatus.java
org.cicadasong.samples.webimageplayer.Preferences.java
org.cicadasong.samples.webimageplayer.WebImagePlayerSettingsActivity.java
org.cicadasong.samples.webimageplayer.WebImagePlayer.java