am e954cb0b: Update NotificationListener sample after API update
* commit 'e954cb0b14edea915bbcd1040f85371c2d0aa66d':
Update NotificationListener sample after API update
diff --git a/NotificationShowcase/AndroidManifest.xml b/NotificationShowcase/AndroidManifest.xml
index 871bff9..def9061 100644
--- a/NotificationShowcase/AndroidManifest.xml
+++ b/NotificationShowcase/AndroidManifest.xml
@@ -6,6 +6,9 @@
<uses-sdk android:minSdkVersion="4"
android:targetSdkVersion="16" />
+ <uses-permission android:name="android.permission.READ_CONTACTS">
+
+ </uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".NotificationShowcaseActivity"
android:label="@string/app_name">
@@ -14,9 +17,16 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
+ <activity android:name=".FullScreenActivity"
+ android:label="@string/full_screen_name">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ </intent-filter>
+ </activity>
<service android:name=".NotificationService"/>
<service android:name=".UpdateService"/>
<service android:name=".ProgressService"/>
+ <service android:name=".PhoneService"/>
<service android:name=".ToastService"/>
</application>
</manifest>
diff --git a/NotificationShowcase/res/layout/full_screen.xml b/NotificationShowcase/res/layout/full_screen.xml
new file mode 100644
index 0000000..6ff7552
--- /dev/null
+++ b/NotificationShowcase/res/layout/full_screen.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent">
+
+ <ImageView
+ android:layout_height="match_parent"
+ android:layout_width="match_parent"
+ android:src="@drawable/page_hed"
+ android:onClick="dismiss"
+ />
+</FrameLayout>
\ No newline at end of file
diff --git a/NotificationShowcase/res/values/strings.xml b/NotificationShowcase/res/values/strings.xml
index c514e2a..80bf103 100644
--- a/NotificationShowcase/res/values/strings.xml
+++ b/NotificationShowcase/res/values/strings.xml
@@ -4,4 +4,7 @@
<string name="app_name">NotificationShowcase</string>
<string name="post_button_label">Post Notifications</string>
<string name="remove_button_label">Remove Notifications</string>
+ <string name="answered">call answered</string>
+ <string name="ignored">call ignored</string>
+ <string name="full_screen_name">Full Screen Activity</string>
</resources>
diff --git a/NotificationShowcase/src/com/android/example/notificationshowcase/FullScreenActivity.java b/NotificationShowcase/src/com/android/example/notificationshowcase/FullScreenActivity.java
new file mode 100644
index 0000000..8286f69
--- /dev/null
+++ b/NotificationShowcase/src/com/android/example/notificationshowcase/FullScreenActivity.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2013 The 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.android.example.notificationshowcase;
+
+import android.app.Activity;
+import android.app.NotificationManager;
+import android.app.PendingIntent;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import android.view.View;
+
+public class FullScreenActivity extends Activity {
+ private static final String TAG = "NotificationShowcase";
+
+ public static final String EXTRA_ID = "id";
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.full_screen);
+ final Intent intent = getIntent();
+ if (intent != null && intent.hasExtra(EXTRA_ID)) {
+ final int id = intent.getIntExtra(EXTRA_ID, -1);
+ if (id >= 0) {
+ NotificationManager noMa =
+ (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
+ noMa.cancel(NotificationService.NOTIFICATION_ID + id);
+ }
+ }
+ }
+
+ public void dismiss(View v) {
+ finish();
+ }
+
+ public static PendingIntent getPendingIntent(Context context, int id) {
+ Intent fullScreenIntent = new Intent(context, FullScreenActivity.class);
+ fullScreenIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+
+ fullScreenIntent.putExtra(EXTRA_ID, id);
+ PendingIntent pi = PendingIntent.getActivity(
+ context, 22, fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);
+ return pi;
+ }
+}
diff --git a/NotificationShowcase/src/com/android/example/notificationshowcase/NotificationService.java b/NotificationShowcase/src/com/android/example/notificationshowcase/NotificationService.java
index 9996081..0b83fa0 100644
--- a/NotificationShowcase/src/com/android/example/notificationshowcase/NotificationService.java
+++ b/NotificationShowcase/src/com/android/example/notificationshowcase/NotificationService.java
@@ -20,17 +20,22 @@
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
+import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
+import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
+import android.provider.ContactsContract;
import android.support.v4.app.NotificationCompat;
import android.text.SpannableString;
+import android.text.TextUtils;
import android.text.style.StyleSpan;
+import android.util.Log;
import android.view.View;
import java.util.ArrayList;
@@ -73,6 +78,34 @@
public static Notification makeBigTextNotification(Context context, int update, int id,
long when) {
+ String personUri = null;
+ Cursor c = null;
+ try {
+ String[] projection = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.LOOKUP_KEY };
+ String selections = ContactsContract.Contacts.DISPLAY_NAME + " = 'Mike Cleron'";
+ final ContentResolver contentResolver = context.getContentResolver();
+ c = contentResolver.query(ContactsContract.Contacts.CONTENT_URI,
+ projection, selections, null, null);
+ if (c != null && c.getCount() > 0) {
+ c.moveToFirst();
+ int lookupIdx = c.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY);
+ int idIdx = c.getColumnIndex(ContactsContract.Contacts._ID);
+ String lookupKey = c.getString(lookupIdx);
+ long contactId = c.getLong(idIdx);
+ Uri lookupUri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
+ personUri = lookupUri.toString();
+ }
+ } finally {
+ if (c != null) {
+ c.close();
+ }
+ }
+ if (TextUtils.isEmpty(personUri)) {
+ Log.w(TAG, "failed to find contact for Mike Cleron");
+ } else {
+ Log.w(TAG, "Mike Cleron is " + personUri);
+ }
+
String addendum = update > 0 ? "(updated) " : "";
String longSmsText = "Hey, looks like\nI'm getting kicked out of this conference" +
" room";
@@ -86,7 +119,7 @@
}
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.bigText(addendum + longSmsText);
- NotificationCompat.Builder bigTextNotification = new NotificationCompat.Builder(context)
+ Notification bigText = new NotificationCompat.Builder(context)
.setContentTitle(addendum + "Mike Cleron")
.setContentIntent(ToastService.getPendingIntent(context, "Clicked on bigText"))
.setContentText(addendum + longSmsText)
@@ -98,8 +131,11 @@
"update: " + update,
UpdateService.getPendingIntent(context, update + 1, id, when))
.setSmallIcon(R.drawable.stat_notify_talk_text)
- .setStyle(bigTextStyle);
- return bigTextNotification.build();
+ .setStyle(bigTextStyle)
+ .setDefaults(Notification.DEFAULT_SOUND)
+ .addPerson(personUri)
+ .build();
+ return bigText;
}
public static Notification makeUploadNotification(Context context, int progress, long when) {
@@ -127,21 +163,23 @@
long uploadWhen = System.currentTimeMillis();
mNotifications.add(makeUploadNotification(this, 10, uploadWhen));
+ int phoneId = mNotifications.size();
+ final PendingIntent fullscreenIntent = FullScreenActivity.getPendingIntent(this, phoneId);
Notification phoneCall = new NotificationCompat.Builder(this)
.setContentTitle("Incoming call")
.setContentText("Matias Duarte")
.setLargeIcon(getBitmap(this, R.drawable.matias_hed))
.setSmallIcon(R.drawable.stat_sys_phone_call)
- .setDefaults(Notification.DEFAULT_SOUND)
.setPriority(NotificationCompat.PRIORITY_MAX)
- .setContentIntent(ToastService.getPendingIntent(this, "Clicked on Matias"))
+ .setContentIntent(fullscreenIntent)
+ .setFullScreenIntent(fullscreenIntent, true)
.addAction(R.drawable.ic_dial_action_call, "Answer",
- ToastService.getPendingIntent(this, "call answered"))
+ PhoneService.getPendingIntent(this, phoneId, PhoneService.ACTION_ANSWER))
.addAction(R.drawable.ic_end_call, "Ignore",
- ToastService.getPendingIntent(this, "call ignored"))
- .setAutoCancel(true)
+ PhoneService.getPendingIntent(this, phoneId, PhoneService.ACTION_IGNORE))
+ .setOngoing(true)
+ .addPerson(Uri.fromParts("tel", "1 (617) 555-1212", null).toString())
.build();
- phoneCall.flags |= Notification.FLAG_INSISTENT;
mNotifications.add(phoneCall);
mNotifications.add(new NotificationCompat.Builder(this)
diff --git a/NotificationShowcase/src/com/android/example/notificationshowcase/PhoneService.java b/NotificationShowcase/src/com/android/example/notificationshowcase/PhoneService.java
new file mode 100644
index 0000000..0b4f3bd
--- /dev/null
+++ b/NotificationShowcase/src/com/android/example/notificationshowcase/PhoneService.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2013 The 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.android.example.notificationshowcase;
+
+import android.app.IntentService;
+import android.app.NotificationManager;
+import android.app.PendingIntent;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Handler;
+import android.util.Log;
+import android.widget.Toast;
+
+public class PhoneService extends IntentService {
+
+ private static final String TAG = "PhoneService";
+
+ public static final String ACTION_ANSWER = "answer";
+ public static final String ACTION_IGNORE = "ignore";
+
+ public static final String EXTRA_ID = "id";
+
+ private Handler handler;
+
+ public PhoneService() {
+ super(TAG);
+ }
+ public PhoneService(String name) {
+ super(name);
+ }
+
+ @Override
+ public int onStartCommand(Intent intent, int flags, int startId) {
+ handler = new Handler();
+ return super.onStartCommand(intent, flags, startId);
+ }
+
+ @Override
+ protected void onHandleIntent(Intent intent) {
+ Log.v(TAG, "clicked a thing! intent=" + intent.toString());
+ int res = ACTION_ANSWER.equals(intent.getAction()) ? R.string.answered : R.string.ignored;
+ final String text = getString(res);
+ final int id = intent.getIntExtra(EXTRA_ID, -1);
+ handler.post(new Runnable() {
+ @Override
+ public void run() {
+ Toast.makeText(PhoneService.this, text, Toast.LENGTH_LONG).show();
+ if (id >= 0) {
+ NotificationManager noMa =
+ (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
+ noMa.cancel(NotificationService.NOTIFICATION_ID + id);
+ }
+ Log.v(TAG, "phone toast " + text);
+ }
+ });
+ }
+
+ public static PendingIntent getPendingIntent(Context context, int id, String action) {
+ Intent phoneIntent = new Intent(context, PhoneService.class);
+ phoneIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ phoneIntent.setAction(action);
+ phoneIntent.putExtra(EXTRA_ID, id);
+ PendingIntent pi = PendingIntent.getService(
+ context, 58, phoneIntent, PendingIntent.FLAG_UPDATE_CURRENT);
+ return pi;
+ }
+}