diff --git a/QKSMS/src/main/java/com/moez/QKSMS/receiver/MessagingReceiver.java b/QKSMS/src/main/java/com/moez/QKSMS/receiver/MessagingReceiver.java index 48c30021..6808232f 100644 --- a/QKSMS/src/main/java/com/moez/QKSMS/receiver/MessagingReceiver.java +++ b/QKSMS/src/main/java/com/moez/QKSMS/receiver/MessagingReceiver.java @@ -11,15 +11,20 @@ import android.telephony.SmsMessage; import android.util.Log; import com.moez.QKSMS.common.BlockedConversationHelper; import com.moez.QKSMS.common.ConversationPrefsHelper; +import com.moez.QKSMS.common.utils.PackageUtils; import com.moez.QKSMS.data.Message; import com.moez.QKSMS.service.NotificationService; import com.moez.QKSMS.transaction.NotificationManager; import com.moez.QKSMS.transaction.SmsHelper; +import com.moez.QKSMS.ui.settings.SettingsFragment; import org.mistergroup.muzutozvednout.ShouldIAnswerBinder; public class MessagingReceiver extends BroadcastReceiver { private final String TAG = "MessagingReceiver"; + private Context mContext; + private SharedPreferences mPrefs; + private String mAddress; private String mBody; private long mDate; @@ -28,9 +33,11 @@ public class MessagingReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { + Log.i(TAG, "onReceive"); abortBroadcast(); - Log.i(TAG, "Received text message"); + mContext = context; + mPrefs = PreferenceManager.getDefaultSharedPreferences(context); if (intent.getExtras() != null) { Object[] pdus = (Object[]) intent.getExtras().get("pdus"); @@ -53,65 +60,76 @@ public class MessagingReceiver extends BroadcastReceiver { mAddress = sms.getDisplayOriginatingAddress(); 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); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - ConversationPrefsHelper conversationPrefs = new ConversationPrefsHelper(context, message.getThreadId()); + ShouldIAnswerBinder shouldIAnswerBinder = new ShouldIAnswerBinder(); + shouldIAnswerBinder.setCallback(new ShouldIAnswerBinder.Callback() { + @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 - // 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(prefs, mAddress)) { - BlockedConversationHelper.unblockFutureConversation(prefs, mAddress); - BlockedConversationHelper.blockConversation(prefs, message.getThreadId()); - message.markSeen(); - BlockedConversationHelper.FutureBlockedConversationObservable.getInstance().futureBlockedConversationReceived(); + @Override + public void onServiceConnected() { + try { + shouldIAnswerBinder.getNumberRating(mAddress); + } catch (Exception e) { + e.printStackTrace(); + } + } - // If we have notifications enabled and this conversation isn't blocked - } else if (conversationPrefs.getNotificationsEnabled() && !BlockedConversationHelper.getBlockedConversationIds( - 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); + @Override + public void onServiceDisconnected() { + } + }); - UnreadBadgeService.update(context); - NotificationManager.create(context); - - } 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.bind(context.getApplicationContext()); + } else { + insertMessageAndNotify(); } } + } - ShouldIAnswerBinder shouldIAnswerBinder = new ShouldIAnswerBinder(); - shouldIAnswerBinder.setCallback(new ShouldIAnswerBinder.Callback() { - @Override - public void onNumberRating(String number, int rating) { - Log.d(TAG, "onNumberRating " + number + ": " + String.valueOf(rating)); - shouldIAnswerBinder.unbind(context.getApplicationContext()); - } + private void insertMessageAndNotify() { + mUri = SmsHelper.addMessageToInbox(mContext, mAddress, mBody, mDate); - @Override - public void onServiceConnected() { - try { - shouldIAnswerBinder.getNumberRating(mAddress); - } catch (Exception e) { - e.printStackTrace(); - } - } + Message message = new Message(mContext, mUri); + ConversationPrefsHelper conversationPrefs = new ConversationPrefsHelper(mContext, message.getThreadId()); - @Override - public void onServiceDisconnected() { - } - }); - shouldIAnswerBinder.bind(context.getApplicationContext()); + // The user has set messages from this address to be blocked, but we at the time there weren't any + // 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)) { + 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(); + } } } diff --git a/QKSMS/src/main/java/org/mistergroup/muzutozvednout/ShouldIAnswerBinder.java b/QKSMS/src/main/java/org/mistergroup/muzutozvednout/ShouldIAnswerBinder.java index 36d396ed..8524f814 100644 --- a/QKSMS/src/main/java/org/mistergroup/muzutozvednout/ShouldIAnswerBinder.java +++ b/QKSMS/src/main/java/org/mistergroup/muzutozvednout/ShouldIAnswerBinder.java @@ -15,10 +15,10 @@ import android.os.RemoteException; * Created by Mister Group s.r.o. on 30.11.2015. */ public class ShouldIAnswerBinder { - static final int RATING_UNKNOWN = 0; - static final int RATING_POSITIVE = 1; - static final int RATING_NEGATIVE = 2; - static final int RATING_NEUTRAL = 3; + public static final int RATING_UNKNOWN = 0; + public static final int RATING_POSITIVE = 1; + public static final int RATING_NEGATIVE = 2; + public static final int RATING_NEUTRAL = 3; private Callback callback; public boolean isBound;