mirror of
https://github.com/ArcaneChat/android.git
synced 2026-07-03 14:05:24 +02:00
Merge remote-tracking branch 'origin/main'
This commit is contained in:
@@ -45,6 +45,7 @@ import org.thoughtcrime.securesms.components.reminder.DozeReminder;
|
||||
import org.thoughtcrime.securesms.connect.DcEventCenter;
|
||||
import org.thoughtcrime.securesms.connect.DcHelper;
|
||||
import org.thoughtcrime.securesms.mms.GlideApp;
|
||||
import org.thoughtcrime.securesms.notifications.FcmReceiveService;
|
||||
import org.thoughtcrime.securesms.permissions.Permissions;
|
||||
import org.thoughtcrime.securesms.util.Prefs;
|
||||
import org.thoughtcrime.securesms.util.RelayUtil;
|
||||
@@ -196,6 +197,7 @@ public class ConversationListFragment extends BaseConversationListFragment
|
||||
if (DozeReminder.isEligible(context)) {
|
||||
DozeReminder.addDozeReminderDeviceMsg(context);
|
||||
}
|
||||
FcmReceiveService.waitForRegisterFinished();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -221,6 +223,8 @@ public class ConversationListFragment extends BaseConversationListFragment
|
||||
dcContext.addDeviceMsg("android.notifications-disabled", msg);
|
||||
})
|
||||
.execute();
|
||||
} else {
|
||||
DozeReminder.maybeAskDirectly(activity);
|
||||
}
|
||||
} else {
|
||||
DozeReminder.maybeAskDirectly(activity);
|
||||
|
||||
@@ -18,8 +18,10 @@ import com.b44t.messenger.DcContact;
|
||||
import com.b44t.messenger.DcContext;
|
||||
import com.b44t.messenger.DcMsg;
|
||||
|
||||
import org.thoughtcrime.securesms.ApplicationContext;
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.connect.DcHelper;
|
||||
import org.thoughtcrime.securesms.notifications.FcmReceiveService;
|
||||
import org.thoughtcrime.securesms.util.Prefs;
|
||||
|
||||
@SuppressLint("BatteryLife")
|
||||
@@ -104,8 +106,27 @@ public class DozeReminder {
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isAllChatmail() {
|
||||
for (int accountId : ApplicationContext.dcAccounts.getAll()) {
|
||||
DcContext context = ApplicationContext.dcAccounts.getAccount(accountId);
|
||||
if (!context.isChatmail()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean isPushAvailableAndSufficient() {
|
||||
return isAllChatmail() && FcmReceiveService.getToken() != null;
|
||||
}
|
||||
|
||||
public static void maybeAskDirectly(Context context) {
|
||||
try {
|
||||
if (isPushAvailableAndSufficient()) {
|
||||
// do not set DOZE_ASKED_DIRECTLY, this will happen once FCM is disabled or a non-chatmail is added
|
||||
return;
|
||||
}
|
||||
|
||||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M
|
||||
&& !Prefs.getBooleanPreference(context, Prefs.DOZE_ASKED_DIRECTLY, false)
|
||||
&& ContextCompat.checkSelfPermission(context, Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) == PackageManager.PERMISSION_GRANTED
|
||||
|
||||
@@ -7,6 +7,7 @@ import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.WorkerThread;
|
||||
|
||||
import com.google.android.gms.tasks.Tasks;
|
||||
import com.google.firebase.FirebaseApp;
|
||||
@@ -22,16 +23,19 @@ public class FcmReceiveService extends FirebaseMessagingService {
|
||||
private static final String TAG = FcmReceiveService.class.getSimpleName();
|
||||
private static final Object INIT_LOCK = new Object();
|
||||
private static boolean initialized;
|
||||
private static volatile boolean triedRegistering;
|
||||
private static volatile String prefixedToken;
|
||||
|
||||
public static void register(Context context) {
|
||||
if (Build.VERSION.SDK_INT < 19) {
|
||||
Log.w(TAG, "FCM not available on SDK < 19");
|
||||
triedRegistering = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (FcmReceiveService.prefixedToken != null) {
|
||||
Log.i(TAG, "FCM already registered");
|
||||
triedRegistering = true;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -51,19 +55,33 @@ public class FcmReceiveService extends FirebaseMessagingService {
|
||||
} catch (Exception e) {
|
||||
// we're here usually when FCM is not available and initializeApp() or getToken() failed.
|
||||
Log.w(TAG, "cannot get FCM token for " + BuildConfig.APPLICATION_ID + ": " + e);
|
||||
triedRegistering = true;
|
||||
return;
|
||||
}
|
||||
if (TextUtils.isEmpty(rawToken)) {
|
||||
Log.w(TAG, "got empty FCM token for " + BuildConfig.APPLICATION_ID);
|
||||
triedRegistering = true;
|
||||
return;
|
||||
}
|
||||
|
||||
prefixedToken = addPrefix(rawToken);
|
||||
Log.i(TAG, "FCM token: " + prefixedToken);
|
||||
ApplicationContext.dcAccounts.setPushDeviceToken(prefixedToken);
|
||||
triedRegistering = true;
|
||||
});
|
||||
}
|
||||
|
||||
// wait a until FCM registration got a token or not.
|
||||
// we're calling register() pretty soon and getToken() pretty late on init,
|
||||
// so usually, this should not block anything.
|
||||
// still, waitForRegisterFinished() needs to be called from a background thread.
|
||||
@WorkerThread
|
||||
public static void waitForRegisterFinished() {
|
||||
while (!triedRegistering) {
|
||||
Util.sleep(100);
|
||||
}
|
||||
}
|
||||
|
||||
private static String addPrefix(String rawToken) {
|
||||
return "fcm-" + BuildConfig.APPLICATION_ID + ":" + rawToken;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user