Only use SIA service when setting enabled and app installed

This commit is contained in:
Moez Bhatti
2015-12-10 01:08:35 -05:00
parent a1705b21d5
commit a15449a46f
2 changed files with 74 additions and 56 deletions
@@ -11,15 +11,20 @@ import android.telephony.SmsMessage;
import android.util.Log; import android.util.Log;
import com.moez.QKSMS.common.BlockedConversationHelper; import com.moez.QKSMS.common.BlockedConversationHelper;
import com.moez.QKSMS.common.ConversationPrefsHelper; import com.moez.QKSMS.common.ConversationPrefsHelper;
import com.moez.QKSMS.common.utils.PackageUtils;
import com.moez.QKSMS.data.Message; import com.moez.QKSMS.data.Message;
import com.moez.QKSMS.service.NotificationService; import com.moez.QKSMS.service.NotificationService;
import com.moez.QKSMS.transaction.NotificationManager; import com.moez.QKSMS.transaction.NotificationManager;
import com.moez.QKSMS.transaction.SmsHelper; import com.moez.QKSMS.transaction.SmsHelper;
import com.moez.QKSMS.ui.settings.SettingsFragment;
import org.mistergroup.muzutozvednout.ShouldIAnswerBinder; import org.mistergroup.muzutozvednout.ShouldIAnswerBinder;
public class MessagingReceiver extends BroadcastReceiver { public class MessagingReceiver extends BroadcastReceiver {
private final String TAG = "MessagingReceiver"; private final String TAG = "MessagingReceiver";
private Context mContext;
private SharedPreferences mPrefs;
private String mAddress; private String mAddress;
private String mBody; private String mBody;
private long mDate; private long mDate;
@@ -28,9 +33,11 @@ public class MessagingReceiver extends BroadcastReceiver {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
Log.i(TAG, "onReceive");
abortBroadcast(); abortBroadcast();
Log.i(TAG, "Received text message"); mContext = context;
mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
if (intent.getExtras() != null) { if (intent.getExtras() != null) {
Object[] pdus = (Object[]) intent.getExtras().get("pdus"); Object[] pdus = (Object[]) intent.getExtras().get("pdus");
@@ -53,65 +60,76 @@ public class MessagingReceiver extends BroadcastReceiver {
mAddress = sms.getDisplayOriginatingAddress(); mAddress = sms.getDisplayOriginatingAddress();
mDate = sms.getTimestampMillis(); mDate = sms.getTimestampMillis();
mUri = SmsHelper.addMessageToInbox(context, mAddress, mBody, mDate); if (mPrefs.getBoolean(SettingsFragment.SHOULD_I_ANSWER, false) &&
PackageUtils.isAppInstalled(mContext, "org.mistergroup.muzutozvednout")) {
Message message = new Message(context, mUri); ShouldIAnswerBinder shouldIAnswerBinder = new ShouldIAnswerBinder();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); shouldIAnswerBinder.setCallback(new ShouldIAnswerBinder.Callback() {
ConversationPrefsHelper conversationPrefs = new ConversationPrefsHelper(context, message.getThreadId()); @Override
public void onNumberRating(String number, int rating) {
Log.i(TAG, "onNumberRating " + number + ": " + String.valueOf(rating));
shouldIAnswerBinder.unbind(context.getApplicationContext());
if (rating != ShouldIAnswerBinder.RATING_NEGATIVE) {
insertMessageAndNotify();
}
}
// The user has set messages from this address to be blocked, but we at the time there weren't any @Override
// messages from them already in the database, so we couldn't block any thread URI. Now that we have one, public void onServiceConnected() {
// we can block it, so that the conversation list adapter knows to ignore this thread in the main list try {
if (BlockedConversationHelper.isFutureBlocked(prefs, mAddress)) { shouldIAnswerBinder.getNumberRating(mAddress);
BlockedConversationHelper.unblockFutureConversation(prefs, mAddress); } catch (Exception e) {
BlockedConversationHelper.blockConversation(prefs, message.getThreadId()); e.printStackTrace();
message.markSeen(); }
BlockedConversationHelper.FutureBlockedConversationObservable.getInstance().futureBlockedConversationReceived(); }
// If we have notifications enabled and this conversation isn't blocked @Override
} else if (conversationPrefs.getNotificationsEnabled() && !BlockedConversationHelper.getBlockedConversationIds( public void onServiceDisconnected() {
PreferenceManager.getDefaultSharedPreferences(context)).contains(message.getThreadId())) { }
Intent messageHandlerIntent = new Intent(context, NotificationService.class); });
messageHandlerIntent.putExtra(NotificationService.EXTRA_POPUP, true);
messageHandlerIntent.putExtra(NotificationService.EXTRA_URI, mUri.toString());
context.startService(messageHandlerIntent);
UnreadBadgeService.update(context); shouldIAnswerBinder.bind(context.getApplicationContext());
NotificationManager.create(context); } else {
insertMessageAndNotify();
} else { // We shouldn't show a notification for this message
message.markSeen();
}
if (conversationPrefs.getWakePhoneEnabled()) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "MessagingReceiver");
wakeLock.acquire();
wakeLock.release();
} }
} }
}
ShouldIAnswerBinder shouldIAnswerBinder = new ShouldIAnswerBinder(); private void insertMessageAndNotify() {
shouldIAnswerBinder.setCallback(new ShouldIAnswerBinder.Callback() { mUri = SmsHelper.addMessageToInbox(mContext, mAddress, mBody, mDate);
@Override
public void onNumberRating(String number, int rating) {
Log.d(TAG, "onNumberRating " + number + ": " + String.valueOf(rating));
shouldIAnswerBinder.unbind(context.getApplicationContext());
}
@Override Message message = new Message(mContext, mUri);
public void onServiceConnected() { ConversationPrefsHelper conversationPrefs = new ConversationPrefsHelper(mContext, message.getThreadId());
try {
shouldIAnswerBinder.getNumberRating(mAddress);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override // The user has set messages from this address to be blocked, but we at the time there weren't any
public void onServiceDisconnected() { // messages from them already in the database, so we couldn't block any thread URI. Now that we have one,
} // we can block it, so that the conversation list adapter knows to ignore this thread in the main list
}); if (BlockedConversationHelper.isFutureBlocked(mPrefs, mAddress)) {
shouldIAnswerBinder.bind(context.getApplicationContext()); BlockedConversationHelper.unblockFutureConversation(mPrefs, mAddress);
BlockedConversationHelper.blockConversation(mPrefs, message.getThreadId());
message.markSeen();
BlockedConversationHelper.FutureBlockedConversationObservable.getInstance().futureBlockedConversationReceived();
// If we have notifications enabled and this conversation isn't blocked
} else if (conversationPrefs.getNotificationsEnabled() && !BlockedConversationHelper.getBlockedConversationIds(
PreferenceManager.getDefaultSharedPreferences(mContext)).contains(message.getThreadId())) {
Intent messageHandlerIntent = new Intent(mContext, NotificationService.class);
messageHandlerIntent.putExtra(NotificationService.EXTRA_POPUP, true);
messageHandlerIntent.putExtra(NotificationService.EXTRA_URI, mUri.toString());
mContext.startService(messageHandlerIntent);
UnreadBadgeService.update(mContext);
NotificationManager.create(mContext);
} else { // We shouldn't show a notification for this message
message.markSeen();
}
if (conversationPrefs.getWakePhoneEnabled()) {
PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "MessagingReceiver");
wakeLock.acquire();
wakeLock.release();
}
} }
} }
@@ -15,10 +15,10 @@ import android.os.RemoteException;
* Created by Mister Group s.r.o. on 30.11.2015. * Created by Mister Group s.r.o. on 30.11.2015.
*/ */
public class ShouldIAnswerBinder { public class ShouldIAnswerBinder {
static final int RATING_UNKNOWN = 0; public static final int RATING_UNKNOWN = 0;
static final int RATING_POSITIVE = 1; public static final int RATING_POSITIVE = 1;
static final int RATING_NEGATIVE = 2; public static final int RATING_NEGATIVE = 2;
static final int RATING_NEUTRAL = 3; public static final int RATING_NEUTRAL = 3;
private Callback callback; private Callback callback;
public boolean isBound; public boolean isBound;