Skip to content

Commit

Permalink
Add notification to send exported database.
Browse files Browse the repository at this point in the history
  • Loading branch information
bhandfast committed Feb 3, 2015
1 parent eb6c503 commit 4feab8c
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 10 deletions.
34 changes: 29 additions & 5 deletions app/src/main/java/com/eveningoutpost/dexdrip/Home.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
package com.eveningoutpost.dexdrip;

import android.app.Activity;
import android.app.NotificationManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Paint;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import com.eveningoutpost.dexdrip.Models.ActiveBluetoothDevice;
import com.eveningoutpost.dexdrip.Models.BgReading;
Expand All @@ -24,9 +28,12 @@
import com.eveningoutpost.dexdrip.UtilityModels.BgGraphBuilder;
import com.eveningoutpost.dexdrip.UtilityModels.CollectionServiceStarter;
import com.eveningoutpost.dexdrip.UtilityModels.Intents;
import com.eveningoutpost.dexdrip.UtilityModels.Notifications;
import com.eveningoutpost.dexdrip.utils.DatabaseUtil;
import com.eveningoutpost.dexdrip.utils.ShareNotification;


import java.io.File;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -280,12 +287,29 @@ public boolean onCreateOptionsMenu(Menu menu) {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_export_database) {
new AsyncTask<Void, Void, Void>() {
new AsyncTask<Void, Void, String>() {
@Override
protected Void doInBackground(Void... params) {
DatabaseUtil.saveSql(getBaseContext());
protected String doInBackground(Void... params) {
return DatabaseUtil.saveSql(getBaseContext());
}

@Override
protected void onPostExecute(String filename) {
super.onPostExecute(filename);

final Context ctx = getApplicationContext();

Toast.makeText(ctx, "Export stored at " + filename, Toast.LENGTH_SHORT).show();

final NotificationCompat.Builder n = new NotificationCompat.Builder(ctx);
n.setContentTitle("Export complete");
n.setContentText("Ready to be sent.");
n.setAutoCancel(true);
n.setSmallIcon(R.drawable.ic_action_communication_invert_colors_on);
ShareNotification.viewOrShare("application/octet-stream", Uri.fromFile(new File(filename)), n, ctx);

return null;
final NotificationManager manager = (NotificationManager) ctx.getSystemService(Service.NOTIFICATION_SERVICE);
manager.notify(Notifications.exportCompleteNotificationId, n.build());
}
}.execute();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ public class Notifications {
public static int currentVolume;
public static AudioManager manager;

public static int BgNotificationId = 001;
public static int calibrationNotificationId = 002;
public static int doubleCalibrationNotificationId = 003;
public static int extraCalibrationNotificationId = 004;
public static final int BgNotificationId = 001;
public static final int calibrationNotificationId = 002;
public static final int doubleCalibrationNotificationId = 003;
public static final int extraCalibrationNotificationId = 004;
public static final int exportCompleteNotificationId = 005;

public static void setNotificationSettings(Context context) {
mContext = context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
public class DatabaseUtil {

public static void saveSql(Context context) {
public static String saveSql(Context context) {
try {

final String databaseName = new Configuration.Builder(context).create().getDatabaseName();
Expand Down Expand Up @@ -50,6 +50,8 @@ public static void saveSql(Context context) {
destStream.close();
}
}

return filename;
} catch (final Exception e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.eveningoutpost.dexdrip.utils;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.NotificationCompat;

/**
* Helper class to show a share notificaiton.
*/
public class ShareNotification {

public static void viewOrShare(String mime, Uri uri, NotificationCompat.Builder builder, Context context) {
final Intent viewFileIntent = new Intent(Intent.ACTION_VIEW);
viewFileIntent.setDataAndType(uri, mime);

ResolveInfo matches = context.getPackageManager().resolveActivity(viewFileIntent, PackageManager.MATCH_DEFAULT_ONLY);

if (matches != null) {
final PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, viewFileIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
addShare(builder, mime, uri, context);
}
} else {
final Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType(mime);
final PendingIntent sharePendingIntent = PendingIntent.getActivity(context, 0, shareIntent, PendingIntent.FLAG_UPDATE_CURRENT);

builder.setContentIntent(sharePendingIntent);
}
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private static void addShare(NotificationCompat.Builder notification, String mime, Uri uri, Context context) {
final Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType(mime);
final PendingIntent sharePendingIntent = PendingIntent.getActivity(context, 0, shareIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.addAction(android.R.drawable.ic_menu_share, "Share", sharePendingIntent);
}
}

0 comments on commit 4feab8c

Please sign in to comment.