diff --git a/build.gradle b/build.gradle index be728c6..ea7ec2f 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "22.0.1" - useLibrary 'org.apache.http.legacy' + useLibrary 'org.apache.http.legacy' defaultConfig { applicationId "fr.unix_experience.owncloud_sms" @@ -28,9 +28,10 @@ repositories { dependencies { compile project(':owncloudAndroidLibrary') compile project(':nrzandroidlib') - compile 'com.android.support:support-v13:23.+' - compile 'com.android.support:appcompat-v7:23.+' - compile 'com.android.support:design:23.+' + compile 'com.android.support:support-v13:23.1.1' + compile 'com.android.support:appcompat-v7:23.1.1' + compile 'com.android.support:design:23.1.1' compile 'in.srain.cube:ultra-ptr:1.0.11' compile 'com.github.dmytrodanylyk.android-process-button:library:1.0.4' + compile 'com.android.support:support-v4:23.1.1' } diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index b51593d..7c0637f 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -1,110 +1,115 @@ - + + android:targetSdkVersion="23"/> - + - - - + + + - - - - - + + + + + - - - - + + + + - + + + + + + + android:theme="@style/OcSmsTheme"> + android:exported="false"/> + android:process=":sync"> - + + android:resource="@xml/sync_adapter"/> + android:label="@string/pref_title_sync"> - + - + + android:resource="@xml/owncloud_account_authenticator"/> - + - + - + - + + android:label="@string/title_activity_login"> + android:label="@string/title_activity_select_account"> + android:label="@string/title_activity_general_settings"> + android:label="@string/app_name"> - + - + + android:label="@string/title_activity_select_contact"> diff --git a/src/main/java/fr/unix_experience/owncloud_sms/activities/MainActivity.java b/src/main/java/fr/unix_experience/owncloud_sms/activities/MainActivity.java index 3fb03c4..5068573 100644 --- a/src/main/java/fr/unix_experience/owncloud_sms/activities/MainActivity.java +++ b/src/main/java/fr/unix_experience/owncloud_sms/activities/MainActivity.java @@ -51,7 +51,8 @@ import fr.unix_experience.owncloud_sms.activities.remote_account.AccountListActi import fr.unix_experience.owncloud_sms.engine.ASyncSMSSync.SyncTask; import fr.unix_experience.owncloud_sms.engine.ConnectivityMonitor; import fr.unix_experience.owncloud_sms.engine.SmsFetcher; -import fr.unix_experience.owncloud_sms.notifications.OCSMSNotificationManager; +import fr.unix_experience.owncloud_sms.enums.OCSMSNotificationType; +import fr.unix_experience.owncloud_sms.notifications.OCSMSNotificationUI; public class MainActivity extends AppCompatActivity { @@ -170,8 +171,8 @@ public class MainActivity extends AppCompatActivity { new SmsFetcher(ctx).bufferMessagesSinceDate(smsList, (long) 0); if (smsList.length() > 0) { - OCSMSNotificationManager nMgr = new OCSMSNotificationManager(ctx); - nMgr.setSyncProcessMsg(); + OCSMSNotificationUI.notify(ctx, ctx.getString(R.string.sync_title), + ctx.getString(R.string.sync_inprogress), OCSMSNotificationType.SYNC.ordinal()); new SyncTask(getApplicationContext(), smsList).execute(); } } diff --git a/src/main/java/fr/unix_experience/owncloud_sms/engine/ASyncSMSSync.java b/src/main/java/fr/unix_experience/owncloud_sms/engine/ASyncSMSSync.java index 1c6cf65..81ca71e 100644 --- a/src/main/java/fr/unix_experience/owncloud_sms/engine/ASyncSMSSync.java +++ b/src/main/java/fr/unix_experience/owncloud_sms/engine/ASyncSMSSync.java @@ -27,8 +27,9 @@ import android.util.Log; import org.json.JSONArray; import fr.unix_experience.owncloud_sms.R; +import fr.unix_experience.owncloud_sms.enums.OCSMSNotificationType; import fr.unix_experience.owncloud_sms.exceptions.OCSyncException; -import fr.unix_experience.owncloud_sms.notifications.OCSMSNotificationManager; +import fr.unix_experience.owncloud_sms.notifications.OCSMSNotificationUI; public interface ASyncSMSSync { class SyncTask extends AsyncTask { @@ -44,7 +45,6 @@ public interface ASyncSMSSync { Account[] myAccountList = _accountMgr.getAccountsByType(_context.getString(R.string.account_type)); // Notify that we are syncing SMS - OCSMSNotificationManager nMgr = new OCSMSNotificationManager(_context); for (Account element : myAccountList) { Uri serverURI = Uri.parse(_accountMgr.getUserData(element, "ocURI")); @@ -54,13 +54,14 @@ public interface ASyncSMSSync { try { _client.doPushRequest(_smsList); - nMgr.dropSyncErrorMsg(); + OCSMSNotificationUI.cancel(_context); } catch (OCSyncException e) { Log.e(ASyncSMSSync.TAG, _context.getString(e.getErrorId())); - nMgr.setSyncErrorMsg(_context.getString(e.getErrorId())); + OCSMSNotificationUI.notify(_context, _context.getString(R.string.fatal_error), + e.getMessage(), OCSMSNotificationType.SYNC_FAILED.ordinal()); } } - nMgr.dropSyncProcessMsg(); + OCSMSNotificationUI.cancel(_context); return null; } diff --git a/src/main/java/fr/unix_experience/owncloud_sms/notifications/OCSMSNotificationManager.java b/src/main/java/fr/unix_experience/owncloud_sms/notifications/OCSMSNotificationManager.java deleted file mode 100644 index 4b21595..0000000 --- a/src/main/java/fr/unix_experience/owncloud_sms/notifications/OCSMSNotificationManager.java +++ /dev/null @@ -1,67 +0,0 @@ -package fr.unix_experience.owncloud_sms.notifications; - -/* - * Copyright (c) 2014-2015, Loic Blot - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -import android.content.Context; -import fr.nrz.androidlib.notifications.NrzNotification; -import fr.unix_experience.owncloud_sms.R; -import fr.unix_experience.owncloud_sms.enums.OCSMSNotificationType; - -public class OCSMSNotificationManager { - - public OCSMSNotificationManager(Context context) { - _context = context; - _notification = new NrzNotification(_context, R.drawable.ic_stat_ocsms); - } - - public void setSyncProcessMsg() { - createNotificationIfPossible(OCSMSNotificationType.SYNC, - _context.getString(R.string.sync_title), - _context.getString(R.string.sync_inprogress) - ); - } - - public void dropSyncProcessMsg() { - _notification.cancelNotify(OCSMSNotificationType.SYNC.ordinal()); - } - - public void setSyncErrorMsg(String errMsg) { - createNotificationIfPossible(OCSMSNotificationType.SYNC_FAILED, - _context.getString(R.string.sync_title), - _context.getString(R.string.fatal_error) + "\n" + errMsg - ); - } - - public void dropSyncErrorMsg() { - _notification.cancelNotify(OCSMSNotificationType.SYNC_FAILED.ordinal()); - } - - public void setDebugMsg(String errMsg) { - createNotificationIfPossible(OCSMSNotificationType.DEBUG, - "DEBUG", errMsg - ); - } - - private void createNotificationIfPossible(OCSMSNotificationType nType, String nTitle, String nMsg) { - _notification.createNotify(nType.ordinal(), nTitle, nMsg); - } - - private final Context _context; - private final NrzNotification _notification; - -} diff --git a/src/main/java/fr/unix_experience/owncloud_sms/notifications/OCSMSNotificationUI.java b/src/main/java/fr/unix_experience/owncloud_sms/notifications/OCSMSNotificationUI.java new file mode 100644 index 0000000..253143e --- /dev/null +++ b/src/main/java/fr/unix_experience/owncloud_sms/notifications/OCSMSNotificationUI.java @@ -0,0 +1,111 @@ +package fr.unix_experience.owncloud_sms.notifications; + +import android.annotation.TargetApi; +import android.app.Notification; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.content.Context; +import android.content.Intent; +import android.content.res.Resources; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.net.Uri; +import android.os.Build; +import android.support.v4.app.NotificationCompat; + +import fr.unix_experience.owncloud_sms.R; + +/** + * Helper class for showing and canceling ui + * notifications. + *

+ * This class makes heavy use of the {@link NotificationCompat.Builder} helper + * class to create notifications in a backward-compatible way. + */ +public class OCSMSNotificationUI { + /** + * The unique identifier for this type of notification. + */ + private static final String NOTIFICATION_TAG = "OCSMS_NOTIFICATION"; + + /** + * Shows the notification, or updates a previously shown notification of + * this type, with the given parameters. + * + * @see #cancel(Context) + */ + public static void notify(final Context context, final String titleString, + final String contentString, final int number) { + final Resources res = context.getResources(); + + // This image is used as the notification's large icon (thumbnail). + // TODO: Remove this if your notification has no relevant thumbnail. + final Bitmap picture = BitmapFactory.decodeResource(res, R.drawable.ic_launcher); + + + final String ticker = titleString.substring(0, 20); + final String title = res.getString(R.string.ui_notification_title_template, titleString); + + final NotificationCompat.Builder builder = new NotificationCompat.Builder(context) + + // Set appropriate defaults for the notification light, sound, + // and vibration. + .setDefaults(Notification.DEFAULT_ALL) + + // Set required fields, including the small icon, the + // notification title, and text. + .setSmallIcon(R.drawable.ic_launcher) + .setContentTitle(title) + .setContentText(contentString) + + // All fields below this line are optional. + + // Use a default priority (recognized on devices running Android + // 4.1 or later) + .setPriority(NotificationCompat.PRIORITY_DEFAULT) + + // Provide a large icon, shown with the notification in the + // notification drawer on devices running Android 3.0 or later. + .setLargeIcon(picture) + + // Set ticker text (preview) information for this notification. + //.setTicker(ticker) + + // Show a number. This is useful when stacking notifications of + // a single type. + .setNumber(number) + .setStyle(new NotificationCompat.BigTextStyle() + .bigText(contentString) + .setBigContentTitle(title) + .setSummaryText(titleString)) + .setAutoCancel(true); + + notify(context, builder.build()); + } + + @TargetApi(Build.VERSION_CODES.ECLAIR) + private static void notify(final Context context, final Notification notification) { + final NotificationManager nm = (NotificationManager) context + .getSystemService(Context.NOTIFICATION_SERVICE); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) { + nm.notify(NOTIFICATION_TAG, 0, notification); + } else { + nm.notify(NOTIFICATION_TAG.hashCode(), notification); + } + } + + /** + * Cancels any notifications of this type previously shown using + * {@link #notify(Context, String, int)}. + */ + @TargetApi(Build.VERSION_CODES.ECLAIR) + public static void cancel(final Context context) { + final NotificationManager nm = (NotificationManager) context + .getSystemService(Context.NOTIFICATION_SERVICE); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) { + nm.cancel(NOTIFICATION_TAG, 0); + } else { + nm.cancel(NOTIFICATION_TAG.hashCode()); + } + } +} diff --git a/src/main/java/fr/unix_experience/owncloud_sms/sync_adapters/SmsSyncAdapter.java b/src/main/java/fr/unix_experience/owncloud_sms/sync_adapters/SmsSyncAdapter.java index 8ce55f4..ea15cbd 100644 --- a/src/main/java/fr/unix_experience/owncloud_sms/sync_adapters/SmsSyncAdapter.java +++ b/src/main/java/fr/unix_experience/owncloud_sms/sync_adapters/SmsSyncAdapter.java @@ -26,11 +26,13 @@ import android.content.SyncResult; import android.net.Uri; import android.os.Bundle; import android.util.Log; + import fr.unix_experience.owncloud_sms.R; import fr.unix_experience.owncloud_sms.engine.OCSMSOwnCloudClient; +import fr.unix_experience.owncloud_sms.enums.OCSMSNotificationType; import fr.unix_experience.owncloud_sms.enums.OCSyncErrorType; import fr.unix_experience.owncloud_sms.exceptions.OCSyncException; -import fr.unix_experience.owncloud_sms.notifications.OCSMSNotificationManager; +import fr.unix_experience.owncloud_sms.notifications.OCSMSNotificationUI; public class SmsSyncAdapter extends AbstractThreadedSyncAdapter { @@ -42,18 +44,18 @@ public class SmsSyncAdapter extends AbstractThreadedSyncAdapter { @Override public void onPerformSync(final Account account, final Bundle extras, final String authority, final ContentProviderClient provider, final SyncResult syncResult) { - - final OCSMSNotificationManager nMgr = new OCSMSNotificationManager(getContext()); - // Create client final String ocURI = _accountMgr.getUserData(account, "ocURI"); if (ocURI == null) { - nMgr.setSyncErrorMsg(getContext().getString(R.string.err_sync_account_unparsable)); + OCSMSNotificationUI.notify(getContext(), getContext().getString(R.string.fatal_error), + getContext().getString(R.string.err_sync_account_unparsable), + OCSMSNotificationType.SYNC_FAILED.ordinal()); return; } final Uri serverURI = Uri.parse(ocURI); - nMgr.setSyncProcessMsg(); + OCSMSNotificationUI.notify(getContext(), getContext().getString(R.string.sync_title), + getContext().getString(R.string.sync_inprogress), OCSMSNotificationType.SYNC.ordinal()); final OCSMSOwnCloudClient _client = new OCSMSOwnCloudClient(getContext(), serverURI, _accountMgr.getUserData(account, "ocLogin"), @@ -65,9 +67,10 @@ public class SmsSyncAdapter extends AbstractThreadedSyncAdapter { // and push datas _client.doPushRequest(null); - nMgr.dropSyncErrorMsg(); + OCSMSNotificationUI.cancel(getContext()); } catch (final OCSyncException e) { - nMgr.setSyncErrorMsg(getContext().getString(e.getErrorId())); + OCSMSNotificationUI.notify(getContext(), getContext().getString(R.string.fatal_error), + getContext().getString(e.getErrorId()), OCSMSNotificationType.SYNC_FAILED.ordinal()); if (e.getErrorType() == OCSyncErrorType.IO) { syncResult.stats.numIoExceptions++; } @@ -82,8 +85,7 @@ public class SmsSyncAdapter extends AbstractThreadedSyncAdapter { } } - nMgr.dropSyncProcessMsg(); - + OCSMSNotificationUI.cancel(getContext()); } private final AccountManager _accountMgr; diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index 90964c0..fb06d15 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -116,7 +116,7 @@ -1 - Push SMS on reception + Push SMS on reception Synchronize in Wi-Fi Synchronize in 4G Synchronize in 3G @@ -196,4 +196,9 @@ Contributors and issue\'s reporters Max messages to send per sync Contact list This function is not available yet. + + OcSMS: %1$s + + Share + Reply