diff --git a/PhotoMessage/app/build.gradle b/PhotoMessage/app/build.gradle index b8ce0a1..5655fda 100644 --- a/PhotoMessage/app/build.gradle +++ b/PhotoMessage/app/build.gradle @@ -6,7 +6,7 @@ android { defaultConfig { applicationId "edu.rosehulman.photomessage" - minSdkVersion 15 + minSdkVersion 16 targetSdkVersion 23 versionCode 1 versionName "1.0" diff --git a/PhotoMessage/app/src/main/AndroidManifest.xml b/PhotoMessage/app/src/main/AndroidManifest.xml index f636a11..94ff367 100644 --- a/PhotoMessage/app/src/main/AndroidManifest.xml +++ b/PhotoMessage/app/src/main/AndroidManifest.xml @@ -24,6 +24,12 @@ android:name=".DisplayLabeledPhotoActivity" android:label="@string/display_labeled_photo_activity_text" > + + + + + + diff --git a/PhotoMessage/app/src/main/java/edu/rosehulman/photomessage/MainActivity.java b/PhotoMessage/app/src/main/java/edu/rosehulman/photomessage/MainActivity.java index 694711a..662ec86 100644 --- a/PhotoMessage/app/src/main/java/edu/rosehulman/photomessage/MainActivity.java +++ b/PhotoMessage/app/src/main/java/edu/rosehulman/photomessage/MainActivity.java @@ -1,5 +1,10 @@ package edu.rosehulman.photomessage; +import android.app.AlarmManager; +import android.app.Notification; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; @@ -7,6 +12,7 @@ import android.graphics.Color; import android.net.Uri; import android.os.Bundle; +import android.os.SystemClock; import android.provider.MediaStore; import android.support.v4.app.DialogFragment; import android.support.v4.content.CursorLoader; @@ -24,13 +30,15 @@ public class MainActivity extends AppCompatActivity { - static final String KEY_MESSAGE = "KEY_MESSAGE"; - static final String KEY_IMAGE_FILENAME = "KEY_IMAGE_FILENAME"; static final String KEY_PHOTO_MESSAGE = "KEY_PHOTO_MESSAGE"; static final String KEY_SOON_NOTIFICATION_ID = "KEY_SOON_NOTIFICATION_ID"; static final String KEY_NOTIFICATION = "KEY_NOTIFICATION"; private static final int RC_PHOTO_ACTIVITY = 1; - private static final int PICK_FROM_GALLERY_REQUEST = 2; + private static final int RC_PICK_FROM_GALLERY = 2; + private static final int THUMBNAIL_SIZE = 96; + private static final int NOTIFICATION_ID = 1; + private static final int SOON_NOTIFICATION_ID = 2; + private static final int SECONDS_UNTIL_ALARM = 15; private static PhotoMessage mPhotoMessage = null; private boolean mCanSavePhoto = false; private Bitmap mBitmap; @@ -54,8 +62,6 @@ protected void onCreate(Bundle savedInstanceState) { R.mipmap.ic_launcher); // For debugging mBitmap = BitmapFactory.decodeFile("/storage/emulated/0/Pictures/PhotoMessage/IMG_20150810_132053.jpg"); - - mBitmap = Bitmap.createScaledBitmap(mBitmap, 512, 512, true); mImageView.setImageBitmap(mBitmap); mCanSavePhoto = true; @@ -128,13 +134,20 @@ public boolean onTouchEvent(MotionEvent event) { private void takePhoto() { Log.d(Constants.TAG, "takePhoto() started"); - // TODO: Launch an activity using the camera intent + // DONE: Launch an activity using the camera intent + Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); + Uri uri = PhotoUtils.getOutputMediaUri(getString(R.string.app_name)); + cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri); + Log.d(Constants.TAG, "Path " + uri.getPath()); + startActivityForResult(cameraIntent, RC_PHOTO_ACTIVITY); + mPhotoMessage.setPath(uri.getPath()); } private void loadFromGallery() { Log.d(Constants.TAG, "loadFromGallery() started"); // TODO: Launch the gallery to pick a photo from it. - + Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); + startActivityForResult(galleryIntent, RC_PICK_FROM_GALLERY); } @Override @@ -145,14 +158,27 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == RC_PHOTO_ACTIVITY) { Log.d(Constants.TAG, "back from taking a photo"); + Log.d(Constants.TAG, "Path " + mPhotoMessage.getPath()); // TODO: Get and show the bitmap - + mBitmap = BitmapFactory.decodeFile(mPhotoMessage.getPath()); + //Use the next 2 lines if your camera res is so high, it crashes your app + int width = 512, height = 512; + mBitmap = Bitmap.createScaledBitmap(mBitmap, width, height, true); + mImageView.setImageBitmap(mBitmap); + mCanSavePhoto = true; } - if (requestCode == MainActivity.PICK_FROM_GALLERY_REQUEST) { + if (requestCode == MainActivity.RC_PICK_FROM_GALLERY) { Log.d(Constants.TAG, "Back from the gallery"); // TODO: Get and show the bitmap - + Uri uri = data.getData(); + String realPath = getRealPathFromUri(uri); + mBitmap = BitmapFactory.decodeFile(realPath); + int width = 512, height = 512; + mBitmap = Bitmap.createScaledBitmap(mBitmap, width, height, true); + mImageView.setImageBitmap(mBitmap); + mPhotoMessage.setPath(realPath); + mCanSavePhoto = false; } } @@ -187,10 +213,26 @@ private void notifyNow() { Log.d(Constants.TAG, "setMessage message to send: " + mPhotoMessage); // TODO: Replace this with a notification. - startActivity(displayIntent); + // startActivity(displayIntent); + Notification notification = getNotification(displayIntent); + NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); + manager.notify(NOTIFICATION_ID, notification); } } + private Notification getNotification(final Intent intent) { + int unusedRequestCode = 0; + PendingIntent pendingIntent = PendingIntent.getActivity(this, unusedRequestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT); + + return new Notification.Builder(this) + .setContentTitle(getString(R.string.notification_title)) + .setContentText(mPhotoMessage.getMessage()) + .setSmallIcon(android.R.drawable.ic_menu_camera) + .setLargeIcon(Bitmap.createScaledBitmap(mBitmap, THUMBNAIL_SIZE, THUMBNAIL_SIZE, true)) + .setContentIntent(pendingIntent) + .build(); + } + private void notifyLater() { Log.d(Constants.TAG, "showLater() started"); DialogFragment df = new SetAlarmDialogFragment(); @@ -204,8 +246,17 @@ public void setSoonAlarm() { Log.d(Constants.TAG, "setMessage message to send: " + mPhotoMessage); // TODO: Replace this with a notification that launches via a timer. - startActivity(displayIntent); - } + // startActivity(displayIntent); + Notification notification = getNotification(displayIntent); + Intent notificationIntent = new Intent(this, NotificationBroadcastReceiver.class); + notificationIntent.putExtra(KEY_NOTIFICATION, notification); + notificationIntent.putExtra(KEY_SOON_NOTIFICATION_ID, SOON_NOTIFICATION_ID); + int unusedRequestCode = 0; + PendingIntent pendingIntent = PendingIntent.getBroadcast(this, unusedRequestCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); + long futureInMillis = SystemClock.elapsedRealtime() + SECONDS_UNTIL_ALARM * 1000; + AlarmManager alarmManger = (AlarmManager)getSystemService(Context.ALARM_SERVICE); + alarmManger.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent); + } public void setFixedAlarm(int hour, int minute) { // Pleaceholder if you wanted to try this out (totally optional) diff --git a/PhotoMessage/app/src/main/java/edu/rosehulman/photomessage/NotificationBroadcastReceiver.java b/PhotoMessage/app/src/main/java/edu/rosehulman/photomessage/NotificationBroadcastReceiver.java new file mode 100644 index 0000000..b72e43d --- /dev/null +++ b/PhotoMessage/app/src/main/java/edu/rosehulman/photomessage/NotificationBroadcastReceiver.java @@ -0,0 +1,24 @@ +package edu.rosehulman.photomessage; + +import android.app.Notification; +import android.app.NotificationManager; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; + +/** + * Created by Matt Boutell on 2/1/2016. + * Rose-Hulman Institute of Technology. + * Covered by MIT license. + */ +public class NotificationBroadcastReceiver extends BroadcastReceiver { + + @Override + public void onReceive(Context context, Intent intent) { + NotificationManager manager = (NotificationManager) + context.getSystemService(Context.NOTIFICATION_SERVICE); + Notification notification = intent.getParcelableExtra(MainActivity.KEY_NOTIFICATION); + int id = intent.getIntExtra(MainActivity.KEY_SOON_NOTIFICATION_ID, 0); + manager.notify(id, notification); + } +}