Android Open Source - ZShaolin Service Foreground Compat






From Project

Back to project page ZShaolin.

License

The source code is released under:

GNU General Public License

If you think the Android project ZShaolin 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 Steven Luo/* w  w w  .j  a va  2s  .c o  m*/
 * Copyright (C) 2011 Android Open Source Project
 *
 * 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 com.spartacusrex.spartacuside.util;

import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import android.app.Service;
import android.util.Log;
import android.app.Notification;
import android.app.NotificationManager;

/* Provide startForeground() and stopForeground() compatibility, using the
   current interfaces where available and the deprecated setForeground()
   interface where necessary
   The idea for the implementation comes from an example in the documentation of
   android.app.Service */
public class ServiceForegroundCompat {
    private static Class<?>[] mSetForegroundSig = new Class[] {
        boolean.class };
    private static Class<?>[] mStartForegroundSig = new Class[] {
        int.class, Notification.class };
    private static Class<?>[] mStopForegroundSig = new Class[] {
        boolean.class };

    private Service service;
    private NotificationManager mNM;
    private Method mSetForeground;
    private Method mStartForeground;
    private Method mStopForeground;
    private int notifyId;

    private void invokeMethod(Object receiver, Method method, Object... args) {
        try {
            method.invoke(receiver, args);
        } catch (IllegalAccessException e) {
            // Shouldn't happen, but we have to catch this
            Log.w("ServiceCompat", "Unable to invoke method", e);
        } catch (InvocationTargetException e) {
            /* The methods we call don't throw exceptions -- in general,
               we should throw e.getCause() */
            Log.w("ServiceCompat", "Method threw exception", e.getCause());
        }
    }

    public void startForeground(int id, Notification notification) {
        if (mStartForeground != null) {
            invokeMethod(service, mStartForeground, id, notification);
            return;
        }

        invokeMethod(service, mSetForeground, Boolean.TRUE);
        mNM.notify(id, notification);
        notifyId = id;
    }

    public void stopForeground(boolean removeNotify) {
        if (mStopForeground != null) {
            invokeMethod(service, mStopForeground, removeNotify);
            return;
        }

        if (removeNotify) {
            mNM.cancel(notifyId);
        }
        invokeMethod(service, mSetForeground, Boolean.FALSE);
    }

    public ServiceForegroundCompat(Service service) {
        this.service = service;
        mNM = (NotificationManager)service.getSystemService(service.NOTIFICATION_SERVICE);

        Class<?> clazz = service.getClass();

        try {
            mStartForeground = clazz.getMethod("startForeground", mStartForegroundSig);
            mStopForeground = clazz.getMethod("stopForeground", mStopForegroundSig);
        } catch (NoSuchMethodException e) {
            mStartForeground = mStopForeground = null;
        }

        try {
            mSetForeground = clazz.getMethod("setForeground", mSetForegroundSig);
        } catch (NoSuchMethodException e) {
            mSetForeground = null;
        }

        if (mStartForeground == null && mSetForeground == null) {
            throw new IllegalStateException("Neither startForeground() or setForeground() present!");
        }
    }
}




Java Source Code List

com.spartacusrex.spartacuside.EmulatorView.java
com.spartacusrex.spartacuside.Exec.java
com.spartacusrex.spartacuside.TermDebug.java
com.spartacusrex.spartacuside.TermPreferences.java
com.spartacusrex.spartacuside.TermService.java
com.spartacusrex.spartacuside.TermViewFlipper.java
com.spartacusrex.spartacuside.Term.java
com.spartacusrex.spartacuside.WindowList.java
com.spartacusrex.spartacuside.keyboard.CandidateView.java
com.spartacusrex.spartacuside.keyboard.KeyboardPrefs.java
com.spartacusrex.spartacuside.keyboard.KeyboardSwitcher.java
com.spartacusrex.spartacuside.keyboard.LatinKeyboardView.java
com.spartacusrex.spartacuside.keyboard.LatinKeyboard.java
com.spartacusrex.spartacuside.keyboard.MyKeyboardView.java
com.spartacusrex.spartacuside.keyboard.TerminalKeyboard.java
com.spartacusrex.spartacuside.model.Screen.java
com.spartacusrex.spartacuside.model.TextRenderer.java
com.spartacusrex.spartacuside.model.UpdateCallback.java
com.spartacusrex.spartacuside.session.TermSession.java
com.spartacusrex.spartacuside.session.TerminalEmulator.java
com.spartacusrex.spartacuside.session.TranscriptScreen.java
com.spartacusrex.spartacuside.util.ByteQueue.java
com.spartacusrex.spartacuside.util.ServiceForegroundCompat.java
com.spartacusrex.spartacuside.util.TermSettings.java
com.spartacusrex.spartacuside.util.dialogpref.java
com.spartacusrex.spartacuside.util.hardkeymappings.java
com.spartacusrex.spartacuside.util.keydata.java
com.spartacusrex.spartacuside.web.sockethandler.java
com.spartacusrex.spartacuside.web.webserver.java
org.dyne.zshaolin.Start.java
org.dyne.zshaolin.startup.TerminalIDEPrefs.java
org.dyne.zshaolin.startup.setup.assetextract.java
org.dyne.zshaolin.startup.setup.filemanager.java
org.dyne.zshaolin.startup.installer.java
org.dyne.zshaolin.startup.introscreen.java