1
0
mirror of https://github.com/nerzhul/ownCloud-SMS-App.git synced 2025-06-07 16:06:18 +00:00

Drop our notification manage & use native Android Studio providen and better class

This commit is contained in:
Loic Blot 2016-03-05 19:03:17 +01:00
parent 4ea1875dd5
commit c8f37561cf
8 changed files with 188 additions and 129 deletions

View File

@ -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'
}

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fr.unix_experience.owncloud_sms"
<manifest package="fr.unix_experience.owncloud_sms"
xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="34"
android:versionName="0.20.1"> <!-- From Android 4.0 to 6.0 -->
<uses-sdk
@ -29,6 +29,11 @@
<!-- For backup restauration -->
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<android:uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<android:uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<android:uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"

View File

@ -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();
}
}

View File

@ -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<Void, Void, Void> {
@ -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;
}

View File

@ -1,67 +0,0 @@
package fr.unix_experience.owncloud_sms.notifications;
/*
* Copyright (c) 2014-2015, Loic Blot <loic.blot@unix-experience.fr>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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;
}

View File

@ -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.
* <p/>
* 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());
}
}
}

View File

@ -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;

View File

@ -196,4 +196,9 @@ Contributors and issue\'s reporters</string>
<string name="pref_title_bulk_messages">Max messages to send per sync</string>
<string name="contactinfos_list">Contact list</string>
<string name="function_not_available">This function is not available yet.</string>
<string name="ui_notification_title_template">OcSMS: %1$s</string>
<string name="action_share">Share</string>
<string name="action_reply">Reply</string>
</resources>