Android Open Source - PhoneAsTablet Notification Handler






From Project

Back to project page PhoneAsTablet.

License

The source code is released under:

GNU General Public License

If you think the Android project PhoneAsTablet 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) 2013  Tsapalos Vasilios
 * //from w  w  w  .  j a  v a  2 s .c  o  m
 * This program 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 2
 * of the License, or any later version.
 * 
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package info.bits.phoneastablet.utils;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews;

import info.bits.phoneastablet.R;

/**
 * @author LiTTle
 *         A handler of the notification. This handler enables/disables the notification at the notification area.
 */
public class NotificationHandler {

    private static final int NOTIFICATION_ID = 550220;
    private final Context context;
    private NotificationManager nm;

    /**
     * Prepares the notification to be used.
     */
    public NotificationHandler(Context ctx) {
        // TODO Auto-generated constructor stub
        context = ctx;
    }

    /**
     * Shows the notification at the notification area.
     */
    public void enableNotification() {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setTicker(context.getResources().getString(R.string.notification_ticker))
                .setAutoCancel(false)
                .setContentIntent(PendingIntent.getActivity(context, 0, new Intent(), 0))
                .setContentTitle(context.getString(R.string.app_name))
                .setContentText(context.getString(R.string.notification_private_text))
                .setSmallIcon(R.drawable.ic_launcher);
        nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = builder.build();
        notification.flags |= Notification.FLAG_ONGOING_EVENT;

        RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification);
        //set the button listeners
        setListeners(contentView);
        notification.contentView = contentView;
        nm.notify(NOTIFICATION_ID, notification);

    }

    /**
     * Hides the notification from the notification area.
     */
    public void disableNotification() {
        if (nm != null)
            nm.cancel(NOTIFICATION_ID);
    }

    /**
     * Sets the listeners for the buttons at the notification area.
     *
     * @param view
     */
    public void setListeners(RemoteViews view) {
        //App start listener
        Intent app = new Intent(context, NotificationButtonsHandler.class);
        app.putExtra("DO", "app");
        PendingIntent pApp = PendingIntent.getActivity(context, 0, app, 0);
        view.setOnClickPendingIntent(R.id.app, pApp);

        //default screen size listener
        Intent defaultResolution = new Intent(context, NotificationButtonsHandler.class);
        defaultResolution.putExtra("DO", "default");
        PendingIntent pDefaultResolution = PendingIntent.getActivity(context, 1, defaultResolution, 0);
        view.setOnClickPendingIntent(R.id.default_resolution, pDefaultResolution);

        //custom screen size listener
        Intent customResolution = new Intent(context, NotificationButtonsHandler.class);
        customResolution.putExtra("DO", "custom");
        PendingIntent pCustomResolution = PendingIntent.getActivity(context, 2, customResolution, 0);
        view.setOnClickPendingIntent(R.id.custom_resolution, pCustomResolution);
    }
}




Java Source Code List

info.bits.phoneastablet.ApplicationTest.java
info.bits.phoneastablet.Resolution.java
info.bits.phoneastablet.listeners.MyOnCheckedListener.java
info.bits.phoneastablet.listeners.MyTextWatcher.java
info.bits.phoneastablet.listeners.package-info.java
info.bits.phoneastablet.receivers.BootCompletedReceiver.java
info.bits.phoneastablet.receivers.OrientationReceiver.java
info.bits.phoneastablet.receivers.package-info.java
info.bits.phoneastablet.services.OrientationService.java
info.bits.phoneastablet.services.package-info.java
info.bits.phoneastablet.utils.DatabaseHandler.java
info.bits.phoneastablet.utils.NotificationButtonsHandler.java
info.bits.phoneastablet.utils.NotificationHandler.java
info.bits.phoneastablet.utils.SuCommands.java
info.bits.phoneastablet.utils.package-info.java