forked from upstream-mirrors/quik
Merge branch 'should-I-answer'
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
package com.moez.QKSMS.common.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
|
||||
public class PackageUtils {
|
||||
|
||||
public static boolean isAppInstalled(Context context, String packageName) {
|
||||
try {
|
||||
context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
|
||||
return true;
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,25 +11,33 @@ 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";
|
||||
|
||||
String address;
|
||||
String body;
|
||||
long date;
|
||||
private Context mContext;
|
||||
private SharedPreferences mPrefs;
|
||||
|
||||
Uri uri;
|
||||
private String mAddress;
|
||||
private String mBody;
|
||||
private long mDate;
|
||||
|
||||
private Uri mUri;
|
||||
|
||||
@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");
|
||||
@@ -40,49 +48,88 @@ public class MessagingReceiver extends BroadcastReceiver {
|
||||
|
||||
SmsMessage sms = messages[0];
|
||||
if (messages.length == 1 || sms.isReplace()) {
|
||||
body = sms.getDisplayMessageBody();
|
||||
mBody = sms.getDisplayMessageBody();
|
||||
} else {
|
||||
StringBuilder bodyText = new StringBuilder();
|
||||
for (SmsMessage message : messages) {
|
||||
bodyText.append(message.getMessageBody());
|
||||
}
|
||||
body = bodyText.toString();
|
||||
mBody = bodyText.toString();
|
||||
}
|
||||
|
||||
address = sms.getDisplayOriginatingAddress();
|
||||
date = sms.getTimestampMillis();
|
||||
mAddress = sms.getDisplayOriginatingAddress();
|
||||
mDate = sms.getTimestampMillis();
|
||||
|
||||
uri = SmsHelper.addMessageToInbox(context, address, body, date);
|
||||
if (mPrefs.getBoolean(SettingsFragment.SHOULD_I_ANSWER, false) &&
|
||||
PackageUtils.isAppInstalled(mContext, "org.mistergroup.muzutozvednout")) {
|
||||
|
||||
Message message = new Message(context, uri);
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
if (BlockedConversationHelper.isFutureBlocked(prefs, address)) {
|
||||
BlockedConversationHelper.unblockFutureConversation(prefs, address);
|
||||
BlockedConversationHelper.blockConversation(prefs, message.getThreadId());
|
||||
message.markSeen();
|
||||
BlockedConversationHelper.FutureBlockedConversationObservable.getInstance().futureBlockedConversationReceived();
|
||||
@Override
|
||||
public void onServiceConnected() {
|
||||
try {
|
||||
shouldIAnswerBinder.getNumberRating(mAddress);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
} 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, uri.toString());
|
||||
context.startService(messageHandlerIntent);
|
||||
@Override
|
||||
public void onServiceDisconnected() {
|
||||
}
|
||||
});
|
||||
|
||||
UnreadBadgeService.update(context);
|
||||
NotificationManager.create(context);
|
||||
shouldIAnswerBinder.bind(context.getApplicationContext());
|
||||
} else {
|
||||
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();
|
||||
insertMessageAndNotify();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void insertMessageAndNotify() {
|
||||
mUri = SmsHelper.addMessageToInbox(mContext, mAddress, mBody, mDate);
|
||||
|
||||
Message message = new Message(mContext, mUri);
|
||||
ConversationPrefsHelper conversationPrefs = new ConversationPrefsHelper(mContext, message.getThreadId());
|
||||
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.moez.QKSMS.service;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Service;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.IBinder;
|
||||
import android.os.Message;
|
||||
import android.os.Messenger;
|
||||
import android.os.RemoteException;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.annotation.Nullable;
|
||||
import com.moez.QKSMS.ui.settings.SettingsFragment;
|
||||
|
||||
public class ShouldIAnswerService extends Service {
|
||||
|
||||
private Messenger mMessenger = new Messenger(new BlockingEnabledHandler(this));
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return mMessenger.getBinder();
|
||||
}
|
||||
|
||||
private class BlockingEnabledHandler extends Handler {
|
||||
private Context mContext;
|
||||
|
||||
private BlockingEnabledHandler(Context context) {
|
||||
mContext = context;
|
||||
}
|
||||
|
||||
@SuppressLint("CommitPrefEdits")
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
super.handleMessage(msg);
|
||||
|
||||
Messenger messenger = msg.replyTo;
|
||||
if (messenger != null && (msg.what == 918 || msg.what == 919)) {
|
||||
Bundle data = new Bundle();
|
||||
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
|
||||
if (msg.what == 919) {
|
||||
boolean enabled=msg.getData().getBoolean("enabled");
|
||||
prefs.edit().putBoolean(SettingsFragment.SHOULD_I_ANSWER, enabled).commit();
|
||||
}
|
||||
|
||||
data.putBoolean("blocking_enabled", prefs.getBoolean(SettingsFragment.SHOULD_I_ANSWER, false));
|
||||
|
||||
Message message = new Message();
|
||||
message.what = msg.what;
|
||||
message.setData(data);
|
||||
|
||||
try {
|
||||
messenger.send(message);
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -665,6 +665,10 @@ public class NotificationManager {
|
||||
*/
|
||||
public static void initQuickCompose(Context context, boolean override, boolean overrideCancel) {
|
||||
|
||||
if (sPrefs == null) {
|
||||
init(context);
|
||||
}
|
||||
|
||||
if (sPrefs.getBoolean(SettingsFragment.QUICKCOMPOSE, false) || override) {
|
||||
Intent composeIntent = new Intent(context, QKComposeActivity.class);
|
||||
PendingIntent composePI = PendingIntent.getActivity(context, 9, composeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
|
||||
@@ -12,6 +12,8 @@ import android.content.res.Resources;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.preference.CheckBoxPreference;
|
||||
import android.preference.EditTextPreference;
|
||||
import android.preference.ListPreference;
|
||||
import android.preference.Preference;
|
||||
@@ -36,6 +38,7 @@ import com.moez.QKSMS.common.ListviewHelper;
|
||||
import com.moez.QKSMS.common.LiveViewManager;
|
||||
import com.moez.QKSMS.common.utils.DateFormatter;
|
||||
import com.moez.QKSMS.common.utils.KeyboardUtils;
|
||||
import com.moez.QKSMS.common.utils.PackageUtils;
|
||||
import com.moez.QKSMS.interfaces.LiveView;
|
||||
import com.moez.QKSMS.receiver.NightModeAutoReceiver;
|
||||
import com.moez.QKSMS.transaction.EndlessJabber;
|
||||
@@ -107,6 +110,7 @@ public class SettingsFragment extends PreferenceFragment implements Preference.O
|
||||
public static final String BLOCKED_ENABLED = "pref_key_blocked_enabled";
|
||||
public static final String BLOCKED_SENDERS = "pref_key_blocked_senders";
|
||||
public static final String BLOCKED_FUTURE = "pref_key_block_future";
|
||||
public static final String SHOULD_I_ANSWER = "pref_key_should_i_answer";
|
||||
public static final String MOBILE_ONLY = "pref_key_mobile_only";
|
||||
public static final String COMPOSE_GROUP = "pref_key_compose_group";
|
||||
public static final String SPLIT_SMS = "pref_key_split";
|
||||
@@ -530,6 +534,30 @@ public class SettingsFragment extends PreferenceFragment implements Preference.O
|
||||
case BLOCKED_FUTURE:
|
||||
BlockedNumberDialog.showDialog(mContext);
|
||||
break;
|
||||
case SHOULD_I_ANSWER:
|
||||
final String packageName = "org.mistergroup.muzutozvednout";
|
||||
if (!PackageUtils.isAppInstalled(mContext, packageName)) {
|
||||
String referrer="referrer=utm_source%3Dqksms%26utm_medium%3Dapp%26utm_campaign%3Dqksmssettings";
|
||||
new QKDialog()
|
||||
.setContext(mContext)
|
||||
.setTitle(R.string.dialog_should_i_answer_title)
|
||||
.setMessage(R.string.dialog_should_i_answer_message)
|
||||
.setNegativeButton(R.string.cancel, null)
|
||||
.setPositiveButton(R.string.okay, v -> {
|
||||
try {
|
||||
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageName + "&" + referrer)));
|
||||
} catch (android.content.ActivityNotFoundException anfe) {
|
||||
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + packageName + "&" + referrer)));
|
||||
}
|
||||
})
|
||||
.show();
|
||||
|
||||
new Handler().postDelayed(() -> {
|
||||
mPrefs.edit().putBoolean(SHOULD_I_ANSWER, false).commit();
|
||||
((CheckBoxPreference) preference).setChecked(false);
|
||||
}, 500);
|
||||
}
|
||||
break;
|
||||
case NOTIFICATION_LED_COLOR:
|
||||
mLedColorPickerDialog.show(getActivity().getFragmentManager(), "colorpicker");
|
||||
break;
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
package org.mistergroup.muzutozvednout;
|
||||
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.ServiceConnection;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.IBinder;
|
||||
import android.os.Message;
|
||||
import android.os.Messenger;
|
||||
import android.os.RemoteException;
|
||||
|
||||
/**
|
||||
* Created by Mister Group s.r.o. on 30.11.2015.
|
||||
*/
|
||||
public class ShouldIAnswerBinder {
|
||||
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;
|
||||
|
||||
public interface Callback {
|
||||
void onNumberRating(String number, int rating);
|
||||
|
||||
void onServiceConnected();
|
||||
|
||||
void onServiceDisconnected();
|
||||
}
|
||||
|
||||
public void setCallback(Callback callback) {
|
||||
this.callback = callback;
|
||||
}
|
||||
|
||||
public void bind(Context context) {
|
||||
Intent intent=new Intent("org.mistergroup.muzutozvednout.PublicService");
|
||||
intent.setPackage("org.mistergroup.muzutozvednout");
|
||||
context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
|
||||
}
|
||||
|
||||
public void unbind(Context context) {
|
||||
if ((isBound) && (serviceMessenger != null)) {
|
||||
context.unbindService(mConnection);
|
||||
}
|
||||
}
|
||||
|
||||
public void getNumberRating(String number) throws RemoteException {
|
||||
Message msg = new Message();
|
||||
msg.what = 1;
|
||||
Bundle data = new Bundle();
|
||||
data.putString("number", number);
|
||||
msg.setData(data);
|
||||
msg.replyTo = messenger;
|
||||
serviceMessenger.send(msg);
|
||||
}
|
||||
|
||||
private final Messenger messenger = new Messenger(new IncomingHandler());
|
||||
private static Messenger serviceMessenger = null;
|
||||
|
||||
private ServiceConnection mConnection = new ServiceConnection() {
|
||||
public void onServiceConnected(ComponentName className, IBinder service) {
|
||||
serviceMessenger = new Messenger(service);
|
||||
isBound = true;
|
||||
if (callback != null)
|
||||
callback.onServiceConnected();
|
||||
}
|
||||
|
||||
public void onServiceDisconnected(ComponentName className) {
|
||||
serviceMessenger = null;
|
||||
isBound = false;
|
||||
if (callback != null)
|
||||
callback.onServiceDisconnected();
|
||||
}
|
||||
};
|
||||
|
||||
private class IncomingHandler extends Handler {
|
||||
static final int GET_NUMBER_RATING = 1;
|
||||
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
switch (msg.what) {
|
||||
case GET_NUMBER_RATING:
|
||||
Bundle inData = msg.getData();
|
||||
String number = inData.getString("number");
|
||||
int rating = inData.getInt("rating");
|
||||
if (callback != null)
|
||||
callback.onNumberRating(number, rating);
|
||||
|
||||
break;
|
||||
default:
|
||||
super.handleMessage(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user