access Android_id only on push

Daniel Gultsch created

Change summary

src/main/java/eu/siacs/conversations/services/XmppConnectionService.java | 44 
src/main/java/eu/siacs/conversations/utils/PhoneHelper.java              |  8 
2 files changed, 34 insertions(+), 18 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/services/XmppConnectionService.java 🔗

@@ -913,27 +913,45 @@ public class XmppConnectionService extends Service {
         manageAccountConnectionStates(ACTION_INTERNAL_PING, null);
     }
 
-    private synchronized void manageAccountConnectionStates(final String action, final Bundle extras) {
+    private synchronized void manageAccountConnectionStates(
+            final String action, final Bundle extras) {
         final String pushedAccountHash = extras == null ? null : extras.getString("account");
-        final boolean interactive = Arrays.asList(ACTION_TRY_AGAIN).contains(action);
+        final boolean interactive = java.util.Objects.equals(ACTION_TRY_AGAIN, action);
         WakeLockHelper.acquire(wakeLock);
-        boolean pingNow = ConnectivityManager.CONNECTIVITY_ACTION.equals(action) || (Config.POST_CONNECTIVITY_CHANGE_PING_INTERVAL > 0 && ACTION_POST_CONNECTIVITY_CHANGE.equals(action));
+        boolean pingNow =
+                ConnectivityManager.CONNECTIVITY_ACTION.equals(action)
+                        || (Config.POST_CONNECTIVITY_CHANGE_PING_INTERVAL > 0
+                                && ACTION_POST_CONNECTIVITY_CHANGE.equals(action));
         final HashSet<Account> pingCandidates = new HashSet<>();
-        final String androidId = PhoneHelper.getAndroidId(this);
+        final String androidId = pushedAccountHash == null ? null : PhoneHelper.getAndroidId(this);
         for (final Account account : accounts) {
-            final boolean pushWasMeantForThisAccount = CryptoHelper.getAccountFingerprint(account, androidId).equals(pushedAccountHash);
-            pingNow |= processAccountState(account,
-                    interactive,
-                    "ui".equals(action),
-                    pushWasMeantForThisAccount,
-                    pingCandidates);
+            final boolean pushWasMeantForThisAccount =
+                    androidId != null
+                            && CryptoHelper.getAccountFingerprint(account, androidId)
+                                    .equals(pushedAccountHash);
+            pingNow |=
+                    processAccountState(
+                            account,
+                            interactive,
+                            "ui".equals(action),
+                            pushWasMeantForThisAccount,
+                            pingCandidates);
         }
         if (pingNow) {
-            for (Account account : pingCandidates) {
+            for (final Account account : pingCandidates) {
                 final boolean lowTimeout = isInLowPingTimeoutMode(account);
                 account.getXmppConnection().sendPing();
-                Log.d(Config.LOGTAG, account.getJid().asBareJid() + " send ping (action=" + action + ",lowTimeout=" + lowTimeout + ")");
-                scheduleWakeUpCall(lowTimeout ? Config.LOW_PING_TIMEOUT : Config.PING_TIMEOUT, account.getUuid().hashCode());
+                Log.d(
+                        Config.LOGTAG,
+                        account.getJid().asBareJid()
+                                + " send ping (action="
+                                + action
+                                + ",lowTimeout="
+                                + lowTimeout
+                                + ")");
+                scheduleWakeUpCall(
+                        lowTimeout ? Config.LOW_PING_TIMEOUT : Config.PING_TIMEOUT,
+                        account.getUuid().hashCode());
             }
         }
         WakeLockHelper.release(wakeLock);

src/main/java/eu/siacs/conversations/utils/PhoneHelper.java 🔗

@@ -13,14 +13,12 @@ import android.provider.Settings;
 public class PhoneHelper {
 
     @SuppressLint("HardwareIds")
-    public static String getAndroidId(Context context) {
+    public static String getAndroidId(final Context context) {
         return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
     }
 
-    public static Uri getProfilePictureUri(Context context) {
-        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
-                && context.checkSelfPermission(Manifest.permission.READ_CONTACTS)
-                        != PackageManager.PERMISSION_GRANTED) {
+    public static Uri getProfilePictureUri(final Context context) {
+        if (context.checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
             return null;
         }
         final String[] projection = new String[] {Profile._ID, Profile.PHOTO_URI};