Преглед изворни кода

Initial commit for Notification related projects.

Cristian Tanas пре 11 година
родитељ
комит
e6719181ee
46 измењених фајлова са 953 додато и 0 уклоњено
  1. 27 0
      NotificationBroadcastReceiver/AndroidManifest.xml
  2. BIN
      NotificationBroadcastReceiver/ic_launcher-web.png
  3. BIN
      NotificationBroadcastReceiver/libs/android-support-v4.jar
  4. BIN
      NotificationBroadcastReceiver/res/drawable-hdpi/ic_launcher.png
  5. BIN
      NotificationBroadcastReceiver/res/drawable-mdpi/ic_launcher.png
  6. BIN
      NotificationBroadcastReceiver/res/drawable-xhdpi/ic_launcher.png
  7. BIN
      NotificationBroadcastReceiver/res/drawable-xxhdpi/ic_launcher.png
  8. 39 0
      NotificationBroadcastReceiver/res/layout/activity_main.xml
  9. 36 0
      NotificationBroadcastReceiver/res/layout/custom_notification.xml
  10. BIN
      NotificationBroadcastReceiver/res/raw/efficiency.png
  11. 11 0
      NotificationBroadcastReceiver/res/values-v11/styles.xml
  12. 12 0
      NotificationBroadcastReceiver/res/values-v14/styles.xml
  13. 12 0
      NotificationBroadcastReceiver/res/values/strings.xml
  14. 20 0
      NotificationBroadcastReceiver/res/values/styles.xml
  15. 175 0
      NotificationBroadcastReceiver/src/org/uab/android/notification/broadcastreceiver/LoadImageRunnable.java
  16. 182 0
      NotificationBroadcastReceiver/src/org/uab/android/notification/broadcastreceiver/MainActivity.java
  17. 33 0
      NotificationPendingIntent/AndroidManifest.xml
  18. BIN
      NotificationPendingIntent/ic_launcher-web.png
  19. BIN
      NotificationPendingIntent/libs/android-support-v4.jar
  20. BIN
      NotificationPendingIntent/res/drawable-hdpi/ic_launcher.png
  21. BIN
      NotificationPendingIntent/res/drawable-mdpi/ic_launcher.png
  22. BIN
      NotificationPendingIntent/res/drawable-xhdpi/ic_launcher.png
  23. BIN
      NotificationPendingIntent/res/drawable-xxhdpi/ic_launcher.png
  24. 14 0
      NotificationPendingIntent/res/layout/activity_handle_notification.xml
  25. 15 0
      NotificationPendingIntent/res/layout/activity_main.xml
  26. 11 0
      NotificationPendingIntent/res/values-v11/styles.xml
  27. 12 0
      NotificationPendingIntent/res/values-v14/styles.xml
  28. 10 0
      NotificationPendingIntent/res/values/strings.xml
  29. 20 0
      NotificationPendingIntent/res/values/styles.xml
  30. 13 0
      NotificationPendingIntent/src/org/uab/android/notification/pendingintent/HandleNotificationActivity.java
  31. 80 0
      NotificationPendingIntent/src/org/uab/android/notification/pendingintent/MainActivity.java
  32. 31 0
      NotificationProgressBar/AndroidManifest.xml
  33. BIN
      NotificationProgressBar/ic_launcher-web.png
  34. BIN
      NotificationProgressBar/libs/android-support-v4.jar
  35. BIN
      NotificationProgressBar/res/drawable-hdpi/ic_launcher.png
  36. BIN
      NotificationProgressBar/res/drawable-mdpi/ic_launcher.png
  37. BIN
      NotificationProgressBar/res/drawable-xhdpi/ic_launcher.png
  38. BIN
      NotificationProgressBar/res/drawable-xxhdpi/ic_launcher.png
  39. 14 0
      NotificationProgressBar/res/layout/activity_handle_notification.xml
  40. 15 0
      NotificationProgressBar/res/layout/activity_main.xml
  41. 11 0
      NotificationProgressBar/res/values-v11/styles.xml
  42. 12 0
      NotificationProgressBar/res/values-v14/styles.xml
  43. 10 0
      NotificationProgressBar/res/values/strings.xml
  44. 20 0
      NotificationProgressBar/res/values/styles.xml
  45. 13 0
      NotificationProgressBar/src/org/uab/android/notification/progressbar/HandleNotificationActivity.java
  46. 105 0
      NotificationProgressBar/src/org/uab/android/notification/progressbar/MainActivity.java

+ 27 - 0
NotificationBroadcastReceiver/AndroidManifest.xml

@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="org.uab.android.notification.broadcastreceiver"
+    android:versionCode="1"
+    android:versionName="1.0" >
+
+    <uses-sdk
+        android:minSdkVersion="14"
+        android:targetSdkVersion="19" />
+
+    <application
+        android:allowBackup="true"
+        android:icon="@drawable/ic_launcher"
+        android:label="@string/app_name"
+        android:theme="@style/AppTheme" >
+        <activity
+            android:name=".MainActivity"
+            android:label="@string/app_name" >
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+    </application>
+
+</manifest>

BIN
NotificationBroadcastReceiver/ic_launcher-web.png


BIN
NotificationBroadcastReceiver/libs/android-support-v4.jar


BIN
NotificationBroadcastReceiver/res/drawable-hdpi/ic_launcher.png


BIN
NotificationBroadcastReceiver/res/drawable-mdpi/ic_launcher.png


BIN
NotificationBroadcastReceiver/res/drawable-xhdpi/ic_launcher.png


BIN
NotificationBroadcastReceiver/res/drawable-xxhdpi/ic_launcher.png


+ 39 - 0
NotificationBroadcastReceiver/res/layout/activity_main.xml

@@ -0,0 +1,39 @@
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    tools:context="${relativePackage}.${activityClass}" >
+
+    <Button
+        android:id="@+id/load"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_alignParentBottom="true"
+        android:layout_alignParentLeft="true"
+        android:layout_alignParentRight="true"
+        android:text="@string/loadImage" />
+
+    <ImageView
+        android:id="@+id/contentImage"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_above="@+id/load"
+        android:layout_alignParentLeft="true"
+        android:layout_alignParentRight="true"
+        android:layout_alignParentTop="true"
+        android:src="@drawable/ic_launcher"
+        android:contentDescription="@string/contentImageDescription" />
+    
+    <ProgressBar 
+        android:id="@+id/progressBar"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_above="@+id/load"
+        android:layout_alignParentLeft="true"
+        android:layout_alignParentRight="true"
+        android:layout_alignParentTop="true"
+        android:layout_margin="20dp"
+        android:indeterminate="true"
+        android:visibility="gone"/>
+
+</RelativeLayout>

+ 36 - 0
NotificationBroadcastReceiver/res/layout/custom_notification.xml

@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="utf-8"?>
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:padding="10dp" >
+
+    <ImageView
+        android:id="@+id/notificationIcon"
+        android:layout_width="44dp"
+        android:layout_height="44dp"
+        android:layout_alignParentLeft="true"
+        android:layout_alignParentTop="true"
+        android:src="@android:drawable/ic_dialog_info"
+        android:contentDescription="@string/notificationIconDescription" />
+
+    <TextView
+        android:id="@+id/appName"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_alignParentTop="true"
+        android:layout_marginLeft="16dp"
+        android:layout_marginBottom="8dp"
+        android:layout_toRightOf="@+id/notificationIcon"
+        android:text="@string/app_name"
+        android:textAppearance="?android:attr/textAppearanceMedium"/>
+
+    <TextView
+        android:id="@+id/notificationContent"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_alignBottom="@+id/notificationIcon"
+        android:layout_alignLeft="@+id/appName"
+        android:text="@string/content"
+        android:textAppearance="?android:attr/textAppearanceSmall" />
+
+</RelativeLayout>

BIN
NotificationBroadcastReceiver/res/raw/efficiency.png


+ 11 - 0
NotificationBroadcastReceiver/res/values-v11/styles.xml

@@ -0,0 +1,11 @@
+<resources>
+
+    <!--
+        Base application theme for API 11+. This theme completely replaces
+        AppBaseTheme from res/values/styles.xml on API 11+ devices.
+    -->
+    <style name="AppBaseTheme" parent="android:Theme.Holo.Light">
+        <!-- API 11 theme customizations can go here. -->
+    </style>
+
+</resources>

+ 12 - 0
NotificationBroadcastReceiver/res/values-v14/styles.xml

@@ -0,0 +1,12 @@
+<resources>
+
+    <!--
+        Base application theme for API 14+. This theme completely replaces
+        AppBaseTheme from BOTH res/values/styles.xml and
+        res/values-v11/styles.xml on API 14+ devices.
+    -->
+    <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
+        <!-- API 14 theme customizations can go here. -->
+    </style>
+
+</resources>

+ 12 - 0
NotificationBroadcastReceiver/res/values/strings.xml

@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+
+    <string name="app_name">NotificationBroadcastReceiver</string>
+    <string name="hello_world">Hello world!</string>
+    <string name="loadImage">Load image</string>
+    <string name="contentImageDescription">xkcd - Efficiency</string>
+    <string name="notificationIconDescription">Notification icon</string>
+    <string name="appName">Application name</string>
+    <string name="content">Content</string>
+
+</resources>

+ 20 - 0
NotificationBroadcastReceiver/res/values/styles.xml

@@ -0,0 +1,20 @@
+<resources>
+
+    <!--
+        Base application theme, dependent on API level. This theme is replaced
+        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
+    -->
+    <style name="AppBaseTheme" parent="android:Theme.Light">
+        <!--
+            Theme customizations available in newer API levels can go in
+            res/values-vXX/styles.xml, while customizations related to
+            backward-compatibility can go here.
+        -->
+    </style>
+
+    <!-- Application theme. -->
+    <style name="AppTheme" parent="AppBaseTheme">
+        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
+    </style>
+
+</resources>

+ 175 - 0
NotificationBroadcastReceiver/src/org/uab/android/notification/broadcastreceiver/LoadImageRunnable.java

@@ -0,0 +1,175 @@
+package org.uab.android.notification.broadcastreceiver;
+
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import android.app.NotificationManager;
+import android.app.PendingIntent;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.os.Handler;
+import android.os.Message;
+import android.support.v4.app.NotificationCompat;
+import android.util.Log;
+import android.view.View;
+import android.widget.RemoteViews;
+
+public class LoadImageRunnable implements Runnable {
+	
+	private static final int SIM_NETWORK_DELAY = 5000;
+	private static final int NOTIFICATION_ID = 1;
+	
+	private static boolean success = false;
+	
+	Context context;
+	Handler handler;
+	
+	public LoadImageRunnable(Context context, Handler handler) {
+		this.context = context;
+		this.handler = handler;
+	}
+
+	@Override
+	public void run() {
+		
+		// Send a Message to the MainActivity to set the ProgressBar visibility to View.VISIBLE
+		Message msg = this.handler.obtainMessage(MainActivity.PROGRESS_BAR_VISIBILITY, View.VISIBLE);
+		this.handler.sendMessage(msg);
+		
+		// Simulate a long-running operation
+		try {
+			Thread.sleep(SIM_NETWORK_DELAY);
+		} catch (InterruptedException e) {
+			Log.e("", "");
+		}
+		
+		// Get the image from the raw directory of the application
+		InputStream imageInputStream = context.getResources()
+				.openRawResource(R.raw.efficiency);
+		Bitmap imageBitmap = BitmapFactory.decodeStream(imageInputStream);
+		
+		success = imageBitmap != null;
+		if ( success ) {
+			// If the "download" was successful, save the image to the application's internal storage
+			saveImageToFile(imageBitmap);
+		}
+		
+		// Notify with a broadcast that the image loading process has completed.
+		notifyImageLoaded();
+		
+		try { 
+			imageInputStream.close();
+		} catch (IOException e) {
+			Log.e("", "");
+		}
+		
+		// Send a Message to the MainActivity to set the ProgressBar visibility to View.GONE
+		msg = this.handler.obtainMessage(MainActivity.PROGRESS_BAR_VISIBILITY, View.GONE);
+		this.handler.sendMessage(msg);
+		
+		// Send a Message to the MainActivity to set the ImageView's Bitmap
+		msg = this.handler.obtainMessage(MainActivity.IMAGE_VIEW_SET_BITMAP, imageBitmap);
+		this.handler.sendMessage(msg);
+		
+	}
+	
+	/**
+	 * Save a Bitmap to the application's internal storage for later use.
+	 * 
+	 * @param imageBitmap The Bitmap image to be stored.
+	 */
+	private void saveImageToFile(Bitmap imageBitmap) {
+		
+		try {
+			
+			FileOutputStream imageOutputStream = this.context.openFileOutput(
+					MainActivity.IMAGE_VIEW_CONTENT_PATH,
+					Context.MODE_PRIVATE
+					);
+			imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, imageOutputStream);
+			
+			imageOutputStream.flush();
+			imageOutputStream.close();
+			
+		} catch (FileNotFoundException e) {
+			Log.e("", "");
+			
+		} catch (IOException e) {
+			Log.e("", "");
+		}
+		
+	}
+
+	/**
+	 * Sends an ordered broadcast to inform the loading process has completed.
+	 */
+	private void notifyImageLoaded() {
+		
+		// Sends an ordered broadcast to determine whether MainActivity is
+		// active and in the foreground. Creates a new BroadcastReceiver
+		// to receive a result indicating the state of MainActivity
+		context.sendOrderedBroadcast(
+				new Intent(MainActivity.IMAGE_LOADED_ACTION), 
+				null, 
+				resultReceiver, 	// Register a BroadcastReceiver to get the result back from the invoked receiver
+				null, 
+				0, 
+				null, 
+				null);
+	}
+	
+	/**
+	 * BroadcastReceiver to get back results from the invoked receiver.
+	 * The MainActivity, if in foreground, will set its result to MainActivity.IS_ALIVE.
+	 * Thus, this BroadcastReceiver can determine whether a notification should be issued or not.
+	 */
+	BroadcastReceiver resultReceiver = new BroadcastReceiver() {
+		
+		final String failMsg = "Image loading has failed. Please retry Later.";
+		final String successMsg = "Image loading completed successfully.";
+		
+		@Override
+		public void onReceive(Context context, Intent intent) {
+			
+			// Check the result code
+			if ( getResultCode() != MainActivity.IS_ALIVE ) {
+				
+				// Create a PendingIntent using the MainActivity helper function
+				PendingIntent activityPendingIntent = MainActivity
+						.getCallingPendingIntent(context);
+				
+				// Use R.layout.custom_notification for the layout of the notification 
+				RemoteViews notificationContentView = new RemoteViews(
+						context.getPackageName(), 
+						R.layout.custom_notification);
+				
+				// Set the notification View's text to reflect whether the download completed successfully
+				notificationContentView.setTextViewText(
+						R.id.notificationContent, 
+						success ? successMsg : failMsg);
+				
+				// Builds the notification through the NotificationCompat.Builder class
+				NotificationCompat.Builder notificationBuilder = 
+						new NotificationCompat.Builder(context)
+					.setSmallIcon(android.R.drawable.ic_dialog_info)
+					.setContent(notificationContentView)
+					.setContentIntent(activityPendingIntent)
+					.setAutoCancel(true);
+				
+				// Send the notification
+				NotificationManager notificationManager = (NotificationManager) 
+						context.getSystemService(Context.NOTIFICATION_SERVICE);
+				notificationManager.notify(
+						NOTIFICATION_ID,
+						notificationBuilder.build()
+						);
+			}
+		}
+	};
+
+}

+ 182 - 0
NotificationBroadcastReceiver/src/org/uab/android/notification/broadcastreceiver/MainActivity.java

@@ -0,0 +1,182 @@
+package org.uab.android.notification.broadcastreceiver;
+
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+
+import android.app.Activity;
+import android.app.PendingIntent;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.Message;
+import android.util.Log;
+import android.view.View;
+import android.widget.Button;
+import android.widget.ImageView;
+import android.widget.ProgressBar;
+
+public class MainActivity extends Activity {
+	
+	public static final String IMAGE_VIEW_CONTENT_PATH = "xkcd_latest.png";
+	public static final String IMAGE_LOADED_ACTION = "org.uab.android.notification.broadcastreceiver.IMAGE_LOADED";
+	
+	public static final int IMAGE_VIEW_SET_BITMAP = 0;
+	public static final int PROGRESS_BAR_VISIBILITY = 1;
+	
+	public static final int IS_ALIVE = Activity.RESULT_FIRST_USER;
+	
+	ImageView 	contentImageView;
+	ProgressBar	progressBar;
+	Button		loadImageButton;
+	
+	Handler 	handler = new Handler(new Handler.Callback() {
+		
+		@Override
+		public boolean handleMessage(Message msg) {
+			
+			switch (msg.what) {
+			case PROGRESS_BAR_VISIBILITY:
+				// Set ProgressBar visibility
+				progressBar.setVisibility((Integer) msg.obj);
+				break;
+				
+			case IMAGE_VIEW_SET_BITMAP:
+				// Set ImageView Bitmap
+				contentImageView.setImageBitmap((Bitmap) msg.obj);
+				break;
+
+			default:
+				break;
+			}
+			
+			return true;
+		}
+	});
+	
+	/**
+	 * Returns a PendingIntent to start this Activity from another Context.
+	 * 
+	 * @param context The context from which this method is called.
+	 * 
+	 * @return A PendingIntent to start this Activity.
+	 */
+	public static PendingIntent getCallingPendingIntent(Context context) {
+		
+		Intent startMainActivity = new Intent(context, MainActivity.class);
+		startMainActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);	// This flag is required in order to start a new process if one is not created.
+		
+		PendingIntent startMainActivityPendingIntent = PendingIntent
+				.getActivity(
+						context, 
+						0, 
+						startMainActivity, 
+						PendingIntent.FLAG_UPDATE_CURRENT			// If the PendingIntent already exists, only replace any extra data that has been sent.
+						);
+		
+		return startMainActivityPendingIntent;
+	}
+
+	@Override
+	protected void onCreate(Bundle savedInstanceState) {
+		super.onCreate(savedInstanceState);
+		setContentView(R.layout.activity_main);
+		
+		// Get a reference to the UI elements
+		contentImageView = (ImageView) findViewById(R.id.contentImage);
+		progressBar = (ProgressBar) findViewById(R.id.progressBar);
+		loadImageButton = (Button) findViewById(R.id.load);
+		
+		// Set the Button's OnClickListener
+		loadImageButton.setOnClickListener(new View.OnClickListener() {
+			
+			@Override
+			public void onClick(View v) {
+				
+				// Start a new Thread that simulates a long-running download
+				new Thread(
+						new LoadImageRunnable(MainActivity.this, handler)
+						).start();
+				
+			}
+		});
+		
+		loadImage();
+	}
+	
+	@Override
+	protected void onResume() {
+		super.onResume();
+		
+		// Register a BroadcastReceiver to receive a IMAGE_LOADED_ACTION broadcast
+		IntentFilter intentFilter = new IntentFilter(IMAGE_LOADED_ACTION);
+		registerReceiver(loadImageReceiver, intentFilter);
+	}
+	
+	@Override
+	protected void onPause() {
+		
+		// Unregister the BroadcastReceiver if it has been registered
+		if ( loadImageReceiver != null ) {
+			unregisterReceiver(loadImageReceiver);
+		}
+		
+		super.onPause();
+	}
+	
+	/**
+	 * Load a previously "downloaded" image
+	 */
+	private void loadImage() {
+		
+		// If the image has been previously downloaded by the app, update the ImageView.
+		if ( existsFile(IMAGE_VIEW_CONTENT_PATH) ) {
+			
+			try {
+				
+				FileInputStream imageInputStream = openFileInput(IMAGE_VIEW_CONTENT_PATH);
+				Bitmap image = BitmapFactory.decodeStream(imageInputStream);
+				contentImageView.setImageBitmap(image);
+				
+			} catch (FileNotFoundException e) {
+				Log.e("", "");
+			}
+		}
+	}
+	
+	/**
+	 * Check whether a file with name fileName exists in the application private data.
+	 * 
+	 * @param fileName Name of the file.
+	 * 
+	 * @return A boolean indicating if the file exists or not.
+	 */
+	private boolean existsFile(String fileName) {
+		
+		String[] listOfFiles = fileList();
+		for ( String file : listOfFiles ) {
+			
+			if ( fileName.equals(file) )
+				return true;
+		}
+		
+		return false;
+	}
+	
+	// Broadcast receiver that will handle any IMAGE_LOADED_ACTION broadcast
+	BroadcastReceiver loadImageReceiver = new BroadcastReceiver() {
+		
+		@Override
+		public void onReceive(Context context, Intent intent) {
+			
+			// Make sure this is an ordered broadcast.
+			// Let the sender know that the Intent was received by setting result code to MainActivity.IS_ALIVE
+			if ( isOrderedBroadcast() )
+				setResultCode(IS_ALIVE);
+		}
+	};
+}

+ 33 - 0
NotificationPendingIntent/AndroidManifest.xml

@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="org.uab.android.notification.pendingintent"
+    android:versionCode="1"
+    android:versionName="1.0" >
+
+    <uses-sdk
+        android:minSdkVersion="14"
+        android:targetSdkVersion="19" />
+    
+    <uses-permission android:name="android.permission.VIBRATE"/>
+
+    <application
+        android:allowBackup="true"
+        android:icon="@drawable/ic_launcher"
+        android:label="@string/app_name"
+        android:theme="@style/AppTheme" >
+        <activity
+            android:name=".MainActivity"
+            android:label="@string/app_name" >
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+        <activity
+            android:name=".HandleNotificationActivity"
+            android:label="@string/title_activity_handle_notification" >
+        </activity>
+    </application>
+
+</manifest>

BIN
NotificationPendingIntent/ic_launcher-web.png


BIN
NotificationPendingIntent/libs/android-support-v4.jar


BIN
NotificationPendingIntent/res/drawable-hdpi/ic_launcher.png


BIN
NotificationPendingIntent/res/drawable-mdpi/ic_launcher.png


BIN
NotificationPendingIntent/res/drawable-xhdpi/ic_launcher.png


BIN
NotificationPendingIntent/res/drawable-xxhdpi/ic_launcher.png


+ 14 - 0
NotificationPendingIntent/res/layout/activity_handle_notification.xml

@@ -0,0 +1,14 @@
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    tools:context="${relativePackage}.${activityClass}" >
+
+    <TextView
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_centerHorizontal="true"
+        android:layout_centerVertical="true"
+        android:text="@string/handleNotification" />
+
+</RelativeLayout>

+ 15 - 0
NotificationPendingIntent/res/layout/activity_main.xml

@@ -0,0 +1,15 @@
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    tools:context="${relativePackage}.${activityClass}" >
+
+    <Button
+        android:id="@+id/button1"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_centerHorizontal="true"
+        android:layout_centerVertical="true"
+        android:text="@string/newNotification" />
+
+</RelativeLayout>

+ 11 - 0
NotificationPendingIntent/res/values-v11/styles.xml

@@ -0,0 +1,11 @@
+<resources>
+
+    <!--
+        Base application theme for API 11+. This theme completely replaces
+        AppBaseTheme from res/values/styles.xml on API 11+ devices.
+    -->
+    <style name="AppBaseTheme" parent="android:Theme.Holo.Light">
+        <!-- API 11 theme customizations can go here. -->
+    </style>
+
+</resources>

+ 12 - 0
NotificationPendingIntent/res/values-v14/styles.xml

@@ -0,0 +1,12 @@
+<resources>
+
+    <!--
+        Base application theme for API 14+. This theme completely replaces
+        AppBaseTheme from BOTH res/values/styles.xml and
+        res/values-v11/styles.xml on API 14+ devices.
+    -->
+    <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
+        <!-- API 14 theme customizations can go here. -->
+    </style>
+
+</resources>

+ 10 - 0
NotificationPendingIntent/res/values/strings.xml

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+
+    <string name="app_name">NotificationPendingIntent</string>
+    <string name="hello_world">Hello world!</string>
+    <string name="newNotification">Send notification</string>
+    <string name="title_activity_handle_notification">HandleNotificationActivity</string>
+    <string name="handleNotification">I have received an Intent from the Notification!</string>
+
+</resources>

+ 20 - 0
NotificationPendingIntent/res/values/styles.xml

@@ -0,0 +1,20 @@
+<resources>
+
+    <!--
+        Base application theme, dependent on API level. This theme is replaced
+        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
+    -->
+    <style name="AppBaseTheme" parent="android:Theme.Light">
+        <!--
+            Theme customizations available in newer API levels can go in
+            res/values-vXX/styles.xml, while customizations related to
+            backward-compatibility can go here.
+        -->
+    </style>
+
+    <!-- Application theme. -->
+    <style name="AppTheme" parent="AppBaseTheme">
+        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
+    </style>
+
+</resources>

+ 13 - 0
NotificationPendingIntent/src/org/uab/android/notification/pendingintent/HandleNotificationActivity.java

@@ -0,0 +1,13 @@
+package org.uab.android.notification.pendingintent;
+
+import android.app.Activity;
+import android.os.Bundle;
+
+public class HandleNotificationActivity extends Activity {
+
+	@Override
+	protected void onCreate(Bundle savedInstanceState) {
+		super.onCreate(savedInstanceState);
+		setContentView(R.layout.activity_handle_notification);
+	}
+}

+ 80 - 0
NotificationPendingIntent/src/org/uab/android/notification/pendingintent/MainActivity.java

@@ -0,0 +1,80 @@
+package org.uab.android.notification.pendingintent;
+
+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.support.v4.app.NotificationCompat;
+import android.view.View;
+import android.widget.Button;
+
+public class MainActivity extends Activity {
+	
+	// Notification ID for future updates
+	private static final int RANDOM_NOTIFICATION_ID = 1;
+	
+	// Notification fields
+	private static final String tickerText = "This text appears when first receiving the Notification.";
+	private static final String notificationTitle = "Notification Title";
+	private static final String notificationContent = "You have received a new notification!";
+	
+	// Vibration pattern to make the device vibrate when the Notification is issued
+	private static final long[] VIBRATE_PATTERN = { 0, 200, 200, 300 };
+	
+	// Count the notifications that have been issued from this Activity
+	private int notificationCount = 0;
+	
+	private NotificationManager notificationManager;
+
+	Button	newNotificationButton;
+	
+	@Override
+	protected void onCreate(Bundle savedInstanceState) {
+		super.onCreate(savedInstanceState);
+		setContentView(R.layout.activity_main);
+		
+		newNotificationButton = (Button) findViewById(R.id.button1);
+		
+		// Obtain a reference to the NotificationManager
+		notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
+		
+		// Set the action to be performed when the user clicks on the notification
+		Intent notificationIntent = new Intent(this, HandleNotificationActivity.class);
+		final PendingIntent notificationPendingIntent = PendingIntent
+				.getActivity(
+						this, 							// Context
+						0, 								// Request code
+						notificationIntent, 			// Intent to be launched
+						Intent.FLAG_ACTIVITY_NEW_TASK	// Flags
+						);
+		
+		// Set the Button's OnClickListener
+		newNotificationButton.setOnClickListener(new View.OnClickListener() {
+			
+			@Override
+			public void onClick(View v) {
+				
+				// Build the notification
+				NotificationCompat.Builder notificationBuilder = 
+						new NotificationCompat.Builder(MainActivity.this)
+					.setAutoCancel(true)
+					.setTicker(tickerText)
+					.setContentTitle(notificationTitle)
+					.setContentText(notificationContent)
+					.setNumber(++notificationCount)
+					.setSmallIcon(android.R.drawable.stat_sys_warning)
+					.setContentIntent(notificationPendingIntent)
+					.setVibrate(VIBRATE_PATTERN);
+				
+				// Pass the notification to the NotificationManager
+				notificationManager.notify(
+						RANDOM_NOTIFICATION_ID, 		// Notification ID
+						notificationBuilder.build()		// Notification build previously
+						);
+			}
+		});
+		
+	}
+}

+ 31 - 0
NotificationProgressBar/AndroidManifest.xml

@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="org.uab.android.notification.progressbar"
+    android:versionCode="1"
+    android:versionName="1.0" >
+
+    <uses-sdk
+        android:minSdkVersion="14"
+        android:targetSdkVersion="19" />
+
+    <application
+        android:allowBackup="true"
+        android:icon="@drawable/ic_launcher"
+        android:label="@string/app_name"
+        android:theme="@style/AppTheme" >
+        <activity
+            android:name=".MainActivity"
+            android:label="@string/app_name" >
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+        <activity
+            android:name=".HandleNotificationActivity"
+            android:label="@string/title_activity_handle_notification" >
+        </activity>
+    </application>
+
+</manifest>

BIN
NotificationProgressBar/ic_launcher-web.png


BIN
NotificationProgressBar/libs/android-support-v4.jar


BIN
NotificationProgressBar/res/drawable-hdpi/ic_launcher.png


BIN
NotificationProgressBar/res/drawable-mdpi/ic_launcher.png


BIN
NotificationProgressBar/res/drawable-xhdpi/ic_launcher.png


BIN
NotificationProgressBar/res/drawable-xxhdpi/ic_launcher.png


+ 14 - 0
NotificationProgressBar/res/layout/activity_handle_notification.xml

@@ -0,0 +1,14 @@
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    tools:context="${relativePackage}.${activityClass}" >
+
+    <TextView
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_centerHorizontal="true"
+        android:layout_centerVertical="true"
+        android:text="@string/handleNotification" />
+
+</RelativeLayout>

+ 15 - 0
NotificationProgressBar/res/layout/activity_main.xml

@@ -0,0 +1,15 @@
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    tools:context="${relativePackage}.${activityClass}" >
+
+    <Button
+        android:id="@+id/button1"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_centerHorizontal="true"
+        android:layout_centerVertical="true"
+        android:text="@string/newNotification" />
+
+</RelativeLayout>

+ 11 - 0
NotificationProgressBar/res/values-v11/styles.xml

@@ -0,0 +1,11 @@
+<resources>
+
+    <!--
+        Base application theme for API 11+. This theme completely replaces
+        AppBaseTheme from res/values/styles.xml on API 11+ devices.
+    -->
+    <style name="AppBaseTheme" parent="android:Theme.Holo.Light">
+        <!-- API 11 theme customizations can go here. -->
+    </style>
+
+</resources>

+ 12 - 0
NotificationProgressBar/res/values-v14/styles.xml

@@ -0,0 +1,12 @@
+<resources>
+
+    <!--
+        Base application theme for API 14+. This theme completely replaces
+        AppBaseTheme from BOTH res/values/styles.xml and
+        res/values-v11/styles.xml on API 14+ devices.
+    -->
+    <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
+        <!-- API 14 theme customizations can go here. -->
+    </style>
+
+</resources>

+ 10 - 0
NotificationProgressBar/res/values/strings.xml

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+
+    <string name="app_name">NotificationProgressBar</string>
+    <string name="hello_world">Hello world!</string>
+    <string name="newNotification">Send notification with progress</string>
+    <string name="title_activity_handle_notification">HandleNotificationActivity</string>
+    <string name="handleNotification">I have received an Intent from the Notification!</string>
+
+</resources>

+ 20 - 0
NotificationProgressBar/res/values/styles.xml

@@ -0,0 +1,20 @@
+<resources>
+
+    <!--
+        Base application theme, dependent on API level. This theme is replaced
+        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
+    -->
+    <style name="AppBaseTheme" parent="android:Theme.Light">
+        <!--
+            Theme customizations available in newer API levels can go in
+            res/values-vXX/styles.xml, while customizations related to
+            backward-compatibility can go here.
+        -->
+    </style>
+
+    <!-- Application theme. -->
+    <style name="AppTheme" parent="AppBaseTheme">
+        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
+    </style>
+
+</resources>

+ 13 - 0
NotificationProgressBar/src/org/uab/android/notification/progressbar/HandleNotificationActivity.java

@@ -0,0 +1,13 @@
+package org.uab.android.notification.progressbar;
+
+import android.app.Activity;
+import android.os.Bundle;
+
+public class HandleNotificationActivity extends Activity {
+
+	@Override
+	protected void onCreate(Bundle savedInstanceState) {
+		super.onCreate(savedInstanceState);
+		setContentView(R.layout.activity_handle_notification);
+	}
+}

+ 105 - 0
NotificationProgressBar/src/org/uab/android/notification/progressbar/MainActivity.java

@@ -0,0 +1,105 @@
+package org.uab.android.notification.progressbar;
+
+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.support.v4.app.NotificationCompat;
+import android.view.View;
+import android.widget.Button;
+
+public class MainActivity extends Activity {
+	
+	// Notification ID for future updates
+	private static final int RANDOM_NOTIFICATION_ID = 1;
+	
+	// Notification fields
+	private static final String tickerText = "This text appears when first receiving the Notification.";
+	private static final String notificationTitle = "Notification Title";
+	private static final String notificationContent = "Performing a long-running operation.";
+	private static final String notificationContentOperationFinished = "Operation finished.";
+	
+	// Count the notifications that have been issued from this Activity
+	private int notificationCount = 0;
+	
+	private NotificationManager notificationManager;
+	private NotificationCompat.Builder notificationBuilder;
+
+	Button	newNotificationButton;
+
+	@Override
+	protected void onCreate(Bundle savedInstanceState) {
+		super.onCreate(savedInstanceState);
+		setContentView(R.layout.activity_main);
+		
+		newNotificationButton = (Button) findViewById(R.id.button1);
+		
+		// Obtain a reference to the NotificationManager
+		notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
+		
+		// Set the action to be performed when the user clicks on the notification
+		Intent notificationIntent = new Intent(this, HandleNotificationActivity.class);
+		final PendingIntent notificationPendingIntent = PendingIntent
+				.getActivity(
+						this, 							// Context
+						0, 								// Request code
+						notificationIntent, 			// Intent to be launched
+						Intent.FLAG_ACTIVITY_NEW_TASK	// Flags
+						);
+		
+		// Set the Button's OnClickListener
+		newNotificationButton.setOnClickListener(new View.OnClickListener() {
+			
+			@Override
+			public void onClick(View arg0) {
+				
+				// Build the notification
+				notificationBuilder = new NotificationCompat.Builder(MainActivity.this)
+					.setAutoCancel(true)
+					.setTicker(tickerText)
+					.setContentTitle(notificationTitle)
+					.setContentText(notificationContent)
+					.setNumber(++notificationCount)
+					.setSmallIcon(android.R.drawable.stat_sys_warning)
+					.setContentIntent(notificationPendingIntent);
+
+				// Start a long-running operation in a background Thread
+				new Thread(new Runnable() {
+					
+					@Override
+					public void run() {
+						
+						// Simulate a long-running operation
+						for ( int incr = 0; incr <= 100; incr+=5 ) {
+							
+							// Set the progress indicator to a max value, current completion percentage and determinate state
+							notificationBuilder.setProgress(100, incr, false);
+							
+							// Pass the notification to the NotificationManager
+							notificationManager.notify(
+									RANDOM_NOTIFICATION_ID, 		// Notification ID
+									notificationBuilder.build()		// Notification build previously
+									);
+							
+							try { Thread.sleep(500); } catch (InterruptedException e) {}
+						}
+						
+						// Update the Notification's content
+						notificationBuilder.setContentText(notificationContentOperationFinished);
+						
+						// Remove the ProgressBar
+						notificationBuilder.setProgress(0, 0, false);
+						
+						// Pass the notification to the NotificationManager
+						notificationManager.notify(
+								RANDOM_NOTIFICATION_ID, 		// Notification ID
+								notificationBuilder.build()		// Notification build previously
+								);
+					}
+				}).start();
+			}
+		});
+	}
+}