io.bunnyblue.noticedog.app.notifications.Notification.java Source code

Java tutorial

Introduction

Here is the source code for io.bunnyblue.noticedog.app.notifications.Notification.java

Source

/*
 * Copyright (c) NoticeDog 2017.
 * GNU LESSER GENERAL PUBLIC LICENSE
 *     Version 3, 29 June 2007
 *
 * Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
 * Everyone is permitted to copy and distribute verbatim copies
 * of this license document, but changing it is not allowed.
 *
 *
 * This version of the GNU Lesser General Public License incorporates
 * the terms and conditions of version 3 of the GNU General Public
 * License, supplemented by the additional permissions listed below.
 *
 * 0. Additional Definitions.
 *
 * As used herein, "this License" refers to version 3 of the GNU Lesser
 * General Public License, and the "GNU GPL" refers to version 3 of the GNU
 * General Public License.
 *
 * "The Library" refers to a covered work governed by this License,
 * other than an Application or a Combined Work as defined below.
 *
 * An "Application" is any work that makes use of an interface provided
 * by the Library, but which is not otherwise based on the Library.
 * Defining a subclass of a class defined by the Library is deemed a mode
 * of using an interface provided by the Library.
 *
 * A "Combined Work" is a work produced by combining or linking an
 * Application with the Library.  The particular version of the Library
 * with which the Combined Work was made is also called the "Linked
 * Version".
 *
 * The "Minimal Corresponding Source" for a Combined Work means the
 * Corresponding Source for the Combined Work, excluding any source code
 * for portions of the Combined Work that, considered in isolation, are
 * based on the Application, and not on the Linked Version.
 *
 * The "Corresponding Application Code" for a Combined Work means the
 * object code and/or source code for the Application, including any data
 * and utility programs needed for reproducing the Combined Work from the
 * Application, but excluding the System Libraries of the Combined Work.
 *
 * 1. Exception to Section 3 of the GNU GPL.
 *
 * You may convey a covered work under sections 3 and 4 of this License
 * without being bound by section 3 of the GNU GPL.
 *
 * 2. Conveying Modified Versions.
 *
 * If you modify a copy of the Library, and, in your modifications, a
 * facility refers to a function or data to be supplied by an Application
 * that uses the facility (other than as an argument passed when the
 * facility is invoked), then you may convey a copy of the modified
 * version:
 *
 * a) under this License, provided that you make a good faith effort to
 * ensure that, in the event an Application does not supply the
 * function or data, the facility still operates, and performs
 * whatever part of its purpose remains meaningful, or
 *
 * b) under the GNU GPL, with none of the additional permissions of
 * this License applicable to that copy.
 *
 * 3. Object Code Incorporating Material from Library Header Files.
 *
 * The object code form of an Application may incorporate material from
 * a header file that is part of the Library.  You may convey such object
 * code under terms of your choice, provided that, if the incorporated
 * material is not limited to numerical parameters, data structure
 * layouts and accessors, or small macros, inline functions and templates
 * (ten or fewer lines in length), you do both of the following:
 *
 * a) Give prominent notice with each copy of the object code that the
 * Library is used in it and that the Library and its use are
 * covered by this License.
 *
 * b) Accompany the object code with a copy of the GNU GPL and this license
 * document.
 *
 * 4. Combined Works.
 *
 * You may convey a Combined Work under terms of your choice that,
 * taken together, effectively do not restrict modification of the
 * portions of the Library contained in the Combined Work and reverse
 * engineering for debugging such modifications, if you also do each of
 * the following:
 *
 * a) Give prominent notice with each copy of the Combined Work that
 * the Library is used in it and that the Library and its use are
 * covered by this License.
 *
 * b) Accompany the Combined Work with a copy of the GNU GPL and this license
 * document.
 *
 * c) For a Combined Work that displays copyright notices during
 * execution, include the copyright notice for the Library among
 * these notices, as well as a reference directing the user to the
 * copies of the GNU GPL and this license document.
 *
 * d) Do one of the following:
 *
 * 0) Convey the Minimal Corresponding Source under the terms of this
 * License, and the Corresponding Application Code in a form
 * suitable for, and under terms that permit, the user to
 * recombine or relink the Application with a modified version of
 * the Linked Version to produce a modified Combined Work, in the
 * manner specified by section 6 of the GNU GPL for conveying
 * Corresponding Source.
 *
 * 1) Use a suitable shared library mechanism for linking with the
 * Library.  A suitable mechanism is one that (a) uses at run time
 * a copy of the Library already present on the user's computer
 * system, and (b) will operate properly with a modified version
 * of the Library that is interface-compatible with the Linked
 * Version.
 *
 * e) Provide Installation Information, but only if you would otherwise
 * be required to provide such information under section 6 of the
 * GNU GPL, and only to the extent that such information is
 * necessary to install and execute a modified version of the
 * Combined Work produced by recombining or relinking the
 * Application with a modified version of the Linked Version. (If
 * you use option 4d0, the Installation Information must accompany
 * the Minimal Corresponding Source and Corresponding Application
 * Code. If you use option 4d1, you must provide the Installation
 * Information in the manner specified by section 6 of the GNU GPL
 * for conveying Corresponding Source.)
 *
 * 5. Combined Libraries.
 *
 * You may place library facilities that are a work based on the
 * Library side by side in a single library together with other library
 * facilities that are not Applications and are not covered by this
 * License, and convey such a combined library under terms of your
 * choice, if you do both of the following:
 *
 * a) Accompany the combined library with a copy of the same work based
 * on the Library, uncombined with any other library facilities,
 * conveyed under the terms of this License.
 *
 * b) Give prominent notice with the combined library that part of it
 * is a work based on the Library, and explaining where to find the
 * accompanying uncombined form of the same work.
 *
 * 6. Revised Versions of the GNU Lesser General Public License.
 *
 * The Free Software Foundation may publish revised and/or new versions
 * of the GNU Lesser General Public License from time to time. Such new
 * versions will be similar in spirit to the present version, but may
 * differ in detail to address new problems or concerns.
 *
 * Each version is given a distinguishing version number. If the
 * Library as you received it specifies that a certain numbered version
 * of the GNU Lesser General Public License "or any later version"
 * applies to it, you have the option of following the terms and
 * conditions either of that published version or of any later version
 * published by the Free Software Foundation. If the Library as you
 * received it does not specify a version number of the GNU Lesser
 * General Public License, you may choose any version of the GNU Lesser
 * General Public License ever published by the Free Software Foundation.
 *
 * If the Library as you received it specifies that a proxy can decide
 * whether future versions of the GNU Lesser General Public License shall
 * apply, that proxy's public statement of acceptance of any version is
 * permanent authorization for you to choose that version for the
 * Library.
 */

package io.bunnyblue.noticedog.app.notifications;

import android.annotation.TargetApi;
import android.app.Notification.Action;
import android.app.PendingIntent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class Notification implements Parcelable {
    public static final Creator<Notification> CREATOR = new Creator<Notification>() {
        public Notification createFromParcel(Parcel in) {
            return new Notification(in);
        }

        public Notification[] newArray(int size) {
            return new Notification[size];
        }
    };
    private static final String TAG = "Notification";
    List<Action> actions;
    String contentInfoText;
    String contentText;
    String contentTitle;
    String expandedContentText;
    String expandedContentTitle;
    List<String> expandedInboxText;
    Bitmap expandedLargeIconBig;
    int id;
    String key;
    Bitmap largeIcon;
    String packageName;
    PendingIntent pendingLaunchIntent;
    List<Uri> people;
    Bitmap picture;
    int priority;
    int smallIcon;
    String tag;
    String tickerText;
    long when;

    @TargetApi(19)
    public Notification(String packageName, int id, String tag, String key, android.app.Notification n) {
        this.packageName = packageName;
        this.id = id;
        this.tag = tag;
        this.key = key;
        this.when = n.when;
        this.smallIcon = n.extras.getInt(NotificationCompat.EXTRA_SMALL_ICON, 0);
        this.largeIcon = (Bitmap) n.extras.getParcelable(NotificationCompat.EXTRA_LARGE_ICON);
        if (n.tickerText != null) {
            this.tickerText = n.tickerText.toString();
        }
        this.contentTitle = getStringFromNotificationCharSequence(n, NotificationCompat.EXTRA_TITLE);
        this.contentText = getStringFromNotificationCharSequence(n, NotificationCompat.EXTRA_TEXT);
        this.contentInfoText = getStringFromNotificationCharSequence(n, NotificationCompat.EXTRA_INFO_TEXT);
        this.expandedLargeIconBig = (Bitmap) n.extras.getParcelable(NotificationCompat.EXTRA_LARGE_ICON_BIG);
        this.expandedContentTitle = getStringFromNotificationCharSequence(n, NotificationCompat.EXTRA_TITLE_BIG);
        this.expandedContentText = getStringFromNotificationCharSequence(n, NotificationCompat.EXTRA_SUMMARY_TEXT);
        this.expandedInboxText = getStringArrayFromNotificationCharSequenceArray(n,
                NotificationCompat.EXTRA_TEXT_LINES);
        this.people = getUriArrayFromNotificationParcelableArray(n, NotificationCompat.EXTRA_PEOPLE);
        this.picture = null;
        this.priority = n.priority;
        this.pendingLaunchIntent = n.contentIntent;
        this.actions = new ArrayList();
        if (n.actions != null) {
            for (Action a : n.actions) {
                String title = null;
                if (a.title != null) {
                    title = a.title.toString();
                }
                this.actions.add(new Action(a.icon, title, a.actionIntent));
            }
        }
    }

    public Notification(Parcel in) {
        this.expandedInboxText = new ArrayList();
        this.people = new ArrayList();
        this.actions = new ArrayList();
        this.packageName = in.readString();
        this.id = in.readInt();
        this.tag = in.readString();
        this.key = in.readString();
        this.when = in.readLong();
        this.smallIcon = in.readInt();
        this.largeIcon = (Bitmap) in.readParcelable(Bitmap.class.getClassLoader());
        this.tickerText = in.readString();
        this.contentTitle = in.readString();
        this.contentText = in.readString();
        this.contentInfoText = in.readString();
        this.expandedLargeIconBig = (Bitmap) in.readParcelable(Bitmap.class.getClassLoader());
        this.expandedContentTitle = in.readString();
        this.expandedContentText = in.readString();
        in.readStringList(this.expandedInboxText);
        in.readList(this.people, Uri.class.getClassLoader());
        this.picture = (Bitmap) in.readParcelable(Bitmap.class.getClassLoader());
        this.priority = in.readInt();
        this.pendingLaunchIntent = (PendingIntent) in.readParcelable(PendingIntent.class.getClassLoader());
        in.readList(this.actions, Action.class.getClassLoader());
    }

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel out, int flags) {
        out.writeString(this.packageName);
        out.writeInt(this.id);
        out.writeString(this.tag);
        out.writeString(this.key);
        out.writeLong(this.when);
        out.writeInt(this.smallIcon);
        out.writeParcelable(this.largeIcon, 0);
        out.writeString(this.tickerText);
        out.writeString(this.contentTitle);
        out.writeString(this.contentText);
        out.writeString(this.contentInfoText);
        out.writeParcelable(this.expandedLargeIconBig, 0);
        out.writeString(this.expandedContentTitle);
        out.writeString(this.expandedContentText);
        out.writeStringList(this.expandedInboxText);
        out.writeList(this.people);
        out.writeParcelable(this.picture, 0);
        out.writeInt(this.priority);
        out.writeParcelable(this.pendingLaunchIntent, 0);
        out.writeList(this.actions);
        logParcelSize(out);
    }

    public String getPackageName() {
        return this.packageName;
    }

    public int getId() {
        return this.id;
    }

    public String getTag() {
        return this.tag;
    }

    public String getKey() {
        return this.key;
    }

    public long getWhen() {
        return this.when;
    }

    public int getSmallIcon() {
        return this.smallIcon;
    }

    public Bitmap getLargeIcon() {
        return this.largeIcon;
    }

    public String getTickerText() {
        return this.tickerText;
    }

    public String getContentTitle() {
        return this.contentTitle;
    }

    public String getContentText() {
        return this.contentText;
    }

    public String getContentInfoText() {
        return this.contentInfoText;
    }

    public Bitmap getExpandedLargeIconBig() {
        return this.expandedLargeIconBig;
    }

    public String getExpandedContentTitle() {
        return this.expandedContentTitle;
    }

    public String getExpandedContentText() {
        return this.expandedContentText;
    }

    public List<String> getExpandedInboxText() {
        return this.expandedInboxText;
    }

    public List<Uri> getPeople() {
        return this.people;
    }

    public int getPriority() {
        return this.priority;
    }

    public PendingIntent getPendingLaunchIntent() {
        return this.pendingLaunchIntent;
    }

    public List<Action> getActions() {
        return this.actions;
    }

    public String generateNotificationString() {
        int i;
        StringBuilder sb = new StringBuilder();
        sb.append("Notification {");
        sb.append("    packageName equals " + this.packageName);
        sb.append("    id equals " + this.id);
        sb.append("    tag equals " + this.tag);
        sb.append("    key equals " + this.key);
        sb.append("    when equals " + this.when);
        sb.append("    smallIcon equals " + this.smallIcon);
        if (this.largeIcon != null) {
            sb.append("    largeIcon equals found");
        } else {
            sb.append("    largeIcon equals null");
        }
        sb.append("    tickerText equals " + this.tickerText);
        sb.append("    contentTitle equals " + this.contentTitle);
        sb.append("    contentText equals " + this.contentText);
        sb.append("    contentInfoText equals " + this.contentInfoText);
        if (this.expandedLargeIconBig != null) {
            sb.append("    expandedLargeIconBig equals found");
        } else {
            sb.append("    expandedLargeIconBig equals null");
        }
        sb.append("    expandedContentTitle equals " + this.expandedContentTitle);
        if (this.expandedInboxText.size() == 0) {
            sb.append("    expandedContentTitle equals is empty");
        } else {
            for (i = 0; i < this.expandedInboxText.size(); i++) {
                sb.append("    expandedInboxText[" + i + "] equals " + ((String) this.expandedInboxText.get(i)));
            }
        }
        sb.append("    expandedContentText equals " + this.expandedContentText);
        if (this.people.size() == 0) {
            sb.append("    people equals is empty");
        } else {
            for (i = 0; i < this.people.size(); i++) {
                sb.append("    people[" + i + "] equals " + this.people.get(i));
            }
        }
        if (this.picture != null) {
            sb.append("    picture equals found");
        } else {
            sb.append("    picture equals null");
        }
        if (this.actions.size() == 0) {
            sb.append("    actions is empty");
        } else {
            for (i = 0; i < this.actions.size(); i++) {
                sb.append("    actions[" + i + "] equals " + this.actions.get(i));
            }
        }
        sb.append("}");
        return sb.toString();
    }

    void logParcelSize(Parcel p) {
        Log.d(TAG, "Notification {");
        Log.d(TAG, "    packageName = " + this.packageName);
        Log.d(TAG, "    id = " + this.id);
        Log.d(TAG, "    tag = " + this.tag);
        Log.d(TAG, "    when = " + this.when);
        Log.d(TAG, "}  parcel size in bytes = " + p.dataSize());
    }

    public void logNotification() {
        int i;
        Log.d(TAG, "Notification {");
        Log.d(TAG, "    packageName = " + this.packageName);
        Log.d(TAG, "    id = " + this.id);
        Log.d(TAG, "    tag = " + this.tag);
        Log.d(TAG, "    key = " + this.key);
        Log.d(TAG, "    when = " + this.when);
        Log.d(TAG, "    smallIcon = " + this.smallIcon);
        if (this.largeIcon != null) {
            Log.d(TAG, "    largeIcon = found");
        } else {
            Log.d(TAG, "    largeIcon = null");
        }
        Log.d(TAG, "    tickerText = " + this.tickerText);
        Log.d(TAG, "    contentTitle = " + this.contentTitle);
        Log.d(TAG, "    contentText = " + this.contentText);
        Log.d(TAG, "    contentInfoText = " + this.contentInfoText);
        if (this.expandedLargeIconBig != null) {
            Log.d(TAG, "    expandedLargeIconBig = found");
        } else {
            Log.d(TAG, "    expandedLargeIconBig = null");
        }
        Log.d(TAG, "    expandedContentTitle = " + this.expandedContentTitle);
        if (this.expandedInboxText.size() == 0) {
            Log.d(TAG, "    expandedContentTitle = is empty");
        } else {
            for (i = 0; i < this.expandedInboxText.size(); i++) {
                Log.d(TAG, "    expandedInboxText[" + i + "] = " + ((String) this.expandedInboxText.get(i)));
            }
        }
        Log.d(TAG, "    expandedContentText = " + this.expandedContentText);
        if (this.people.size() == 0) {
            Log.d(TAG, "    people = is empty");
        } else {
            for (i = 0; i < this.people.size(); i++) {
                Log.d(TAG, "    people[" + i + "] = " + this.people.get(i));
            }
        }
        if (this.picture != null) {
            Log.d(TAG, "    picture = found");
        } else {
            Log.d(TAG, "    picture = null");
        }
        if (this.actions.size() == 0) {
            Log.d(TAG, "    actions is empty");
        } else {
            for (i = 0; i < this.actions.size(); i++) {
                Log.d(TAG, "    actions[" + i + "] equals " + this.actions.get(i));
            }
        }
        Log.d(TAG, "}");
    }

    @TargetApi(19)
    String getStringFromNotificationCharSequence(android.app.Notification n, String extra) {
        CharSequence cs = n.extras.getCharSequence(extra);
        if (cs != null) {
            return cs.toString();
        }
        return null;
    }

    @TargetApi(19)
    List<String> getStringArrayFromNotificationCharSequenceArray(android.app.Notification n, String extra) {
        CharSequence[] csa = n.extras.getCharSequenceArray(extra);
        if (csa == null) {
            try {
                return new ArrayList();
            } catch (Exception e) {
                return new ArrayList();
            }
        }
        List<String> stra = new ArrayList(csa.length);
        for (CharSequence cs : csa) {
            if (cs != null) {
                stra.add(cs.toString());
            }
        }
        return stra;
    }

    @TargetApi(19)
    List<Uri> getUriArrayFromNotificationParcelableArray(android.app.Notification n, String extra) {
        Parcelable[] pa = n.extras.getParcelableArray(extra);
        if (pa == null) {
            try {
                return new ArrayList();
            } catch (Exception e) {
                return new ArrayList();
            }
        }
        List<Uri> uris = new ArrayList(pa.length);
        for (Parcelable p : pa) {
            uris.add((Uri) p);
        }
        return uris;
    }
}