android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java |   30 
++---
 android/source/src/java/org/libreoffice/SettingsActivity.java        |   21 ---
 android/source/src/java/org/libreoffice/SettingsListenerModel.java   |   56 
----------
 3 files changed, 15 insertions(+), 92 deletions(-)

New commits:
commit 91fa1f792de325f0fb95e56c59a52d5fc97d860d
Author:     Michael Weghorn <m.wegh...@posteo.de>
AuthorDate: Thu May 16 08:58:40 2024 +0200
Commit:     Michael Weghorn <m.wegh...@posteo.de>
CommitDate: Thu May 16 12:13:51 2024 +0200

    android: Avoid unnecessary prefs processing
    
    When the experimental editing mode preference
    was toggled, it was processed twice:
    
    Toggling that pref requires starting the
    `SettingsActivity`, and when returning to
    `LibreOfficeMainActivity`, its `onResume` method
    was called which updates `mIsExperimentalMode` as needed.
    In addition, the `LibreOfficeMainActivity#onSharedPreferenceChanged`
    callback was called that also processed that preference.
    
    The latter is more "targeted" and only called when a preference
    actually changes.
    Call `updatePreferences()` from there so other preferences
    are also handled and drop the call to `updatePreferences`
    from the `onResume` method, which is no longer needed
    with that in place.
    
    Change-Id: Ic0cacd8ad551137d5aea14cda6838d2ac50de8db
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/167724
    Tested-by: Jenkins
    Reviewed-by: Michael Weghorn <m.wegh...@posteo.de>

diff --git 
a/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java 
b/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java
index 8bacb3f0df72..9f4424f93dab 100644
--- a/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java
+++ b/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java
@@ -458,8 +458,6 @@ public class LibreOfficeMainActivity extends 
AppCompatActivity implements Shared
     protected void onResume() {
         super.onResume();
         Log.i(LOGTAG, "onResume..");
-        // check for config change
-        updatePreferences();
         if (mToolbarController.getEditModeStatus() && isExperimentalMode()) {
             mToolbarController.switchToEditMode();
         } else {
@@ -847,10 +845,7 @@ public class LibreOfficeMainActivity extends 
AppCompatActivity implements Shared
 
     @Override
     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, 
String key) {
-        if (key.matches(ENABLE_EXPERIMENTAL_PREFS_KEY)) {
-            Log.d(LOGTAG, "Editing Preference Changed");
-            mIsExperimentalMode = 
sharedPreferences.getBoolean(ENABLE_EXPERIMENTAL_PREFS_KEY, false);
-        }
+        updatePreferences();
     }
 
     public void promptForPassword() {
commit 3865c81e96432638f3f4cc2ba4eeb481da1c6ebb
Author:     Michael Weghorn <m.wegh...@posteo.de>
AuthorDate: Thu May 16 08:26:14 2024 +0200
Commit:     Michael Weghorn <m.wegh...@posteo.de>
CommitDate: Thu May 16 12:13:45 2024 +0200

    android: Drop indirection listening to pref change
    
    Let `LibreOfficeMainActivity` implement the standard
    Android `SharedPreferences.OnSharedPreferenceChangeListener`
    interface directly, rather than having a custom
    `SettingsListenerModel.OnSettingsPreferenceChangedListener`
    indirection and the `SettingsFragment` implementing the
    standard interface.
    
    This simplifies the code and removes one level of
    indirection.
    
    Drop the now unused `SettingsListenerModel`.
    
    Change-Id: I8c7d6088e711631409f9189bba966439db3daa44
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/167723
    Tested-by: Jenkins
    Reviewed-by: Michael Weghorn <m.wegh...@posteo.de>

diff --git 
a/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java 
b/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java
index 315656b6ef7e..8bacb3f0df72 100644
--- a/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java
+++ b/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java
@@ -57,7 +57,7 @@ import java.util.UUID;
 /**
  * Main activity of the LibreOffice App. It is started in the UI thread.
  */
-public class LibreOfficeMainActivity extends AppCompatActivity implements 
SettingsListenerModel.OnSettingsPreferenceChangedListener {
+public class LibreOfficeMainActivity extends AppCompatActivity implements 
SharedPreferences.OnSharedPreferenceChangeListener {
 
     private static final String LOGTAG = "LibreOfficeMainActivity";
     public static final String ENABLE_EXPERIMENTAL_PREFS_KEY = 
"ENABLE_EXPERIMENTAL";
@@ -125,8 +125,9 @@ public class LibreOfficeMainActivity extends 
AppCompatActivity implements Settin
         Log.w(LOGTAG, "onCreate..");
         super.onCreate(savedInstanceState);
 
-        SettingsListenerModel.getInstance().setListener(this);
         updatePreferences();
+        PreferenceManager.getDefaultSharedPreferences(getApplicationContext())
+            .registerOnSharedPreferenceChangeListener(this);
 
         setContentView(R.layout.activity_main);
 
@@ -492,6 +493,9 @@ public class LibreOfficeMainActivity extends 
AppCompatActivity implements Settin
     @Override
     protected void onDestroy() {
         Log.i(LOGTAG, "onDestroy..");
+        PreferenceManager.getDefaultSharedPreferences(getApplicationContext())
+            .unregisterOnSharedPreferenceChangeListener(this);
+
         LOKitShell.sendCloseEvent();
         mLayerClient.destroy();
         super.onDestroy();
@@ -842,7 +846,7 @@ public class LibreOfficeMainActivity extends 
AppCompatActivity implements Settin
     }
 
     @Override
-    public void settingsPreferenceChanged(SharedPreferences sharedPreferences, 
String key) {
+    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, 
String key) {
         if (key.matches(ENABLE_EXPERIMENTAL_PREFS_KEY)) {
             Log.d(LOGTAG, "Editing Preference Changed");
             mIsExperimentalMode = 
sharedPreferences.getBoolean(ENABLE_EXPERIMENTAL_PREFS_KEY, false);
diff --git a/android/source/src/java/org/libreoffice/SettingsActivity.java 
b/android/source/src/java/org/libreoffice/SettingsActivity.java
index 90d3f95459e4..39102aee5a0f 100644
--- a/android/source/src/java/org/libreoffice/SettingsActivity.java
+++ b/android/source/src/java/org/libreoffice/SettingsActivity.java
@@ -25,7 +25,7 @@ public class SettingsActivity extends FragmentActivity {
             .commit();
     }
 
-    public static class SettingsFragment extends PreferenceFragmentCompat 
implements SharedPreferences.OnSharedPreferenceChangeListener {
+    public static class SettingsFragment extends PreferenceFragmentCompat {
 
         @Override
         public void onCreatePreferences(Bundle savedInstanceState, String 
rootKey) {
@@ -41,25 +41,6 @@ public class SettingsActivity extends FragmentActivity {
                 
generalGroup.removePreference(generalGroup.findPreference("ENABLE_DEVELOPER"));
             }
         }
-
-        @Override
-        public void onResume() {
-            super.onResume();
-            getPreferenceScreen().getSharedPreferences()
-                .registerOnSharedPreferenceChangeListener(this);
-        }
-
-        @Override
-        public void onPause() {
-            super.onPause();
-            getPreferenceScreen().getSharedPreferences()
-                .unregisterOnSharedPreferenceChangeListener(this);
-        }
-
-        @Override
-        public void onSharedPreferenceChanged(SharedPreferences 
sharedPreferences, String key) {
-            
SettingsListenerModel.getInstance().changePreferenceState(sharedPreferences, 
key);
-        }
     }
 }
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/android/source/src/java/org/libreoffice/SettingsListenerModel.java 
b/android/source/src/java/org/libreoffice/SettingsListenerModel.java
deleted file mode 100644
index 1b5a909e1e65..000000000000
--- a/android/source/src/java/org/libreoffice/SettingsListenerModel.java
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- *
- *  * This file is part of the LibreOffice project.
- *  * This Source Code Form is subject to the terms of the Mozilla Public
- *  * License, v. 2.0. If a copy of the MPL was not distributed with this
- *  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
- *
- */
-package org.libreoffice;
-
-import android.content.SharedPreferences;
-
-public class SettingsListenerModel {
-
-    public interface OnSettingsPreferenceChangedListener {
-        void settingsPreferenceChanged(SharedPreferences sharedPreferences, 
String key);
-    }
-
-    private static SettingsListenerModel mInstance;
-    private OnSettingsPreferenceChangedListener mListener;
-    private SharedPreferences sharedPreferences;
-    private String key;
-
-    private SettingsListenerModel() {}
-
-    public static SettingsListenerModel getInstance() {
-        if(mInstance == null) {
-            mInstance = new SettingsListenerModel();
-        }
-        return mInstance;
-    }
-
-    public void setListener(OnSettingsPreferenceChangedListener listener) {
-        mListener = listener;
-    }
-
-    public void changePreferenceState(SharedPreferences sharedPreferences, 
String key) {
-        if(mListener != null) {
-            this.sharedPreferences = sharedPreferences;
-            this.key = key;
-            notifyPreferenceChange(sharedPreferences, key);
-        }
-    }
-
-    public SharedPreferences getSharedPreferences() {
-        return sharedPreferences;
-    }
-
-    public String getKey(){
-        return key;
-    }
-
-    private void notifyPreferenceChange(SharedPreferences preferences, String 
key) {
-        mListener.settingsPreferenceChanged(preferences, key);
-    }
-}
commit 4d4c31349468e68a405887050114d2f3da3547c6
Author:     Michael Weghorn <m.wegh...@posteo.de>
AuthorDate: Thu May 16 07:45:32 2024 +0200
Commit:     Michael Weghorn <m.wegh...@posteo.de>
CommitDate: Thu May 16 12:13:38 2024 +0200

    android: Apply enabled experimental mode when doc open
    
    Use a `mbReadOnlyDoc` member that keeps
    track of the readonly status of the document only
    instead of a `mbISReadOnlyMode` that at the same
    time also was taking into account whether experimental
    editing features were enabled.
    Let `LibreOfficeMainActivity#isReadOnlyMode()` take
    experimental status into account instead.
    
    With this in place, opening a (writable) document
    with experimental editing mode disabled, then enabling
    experimental editing mode makes editing possible after
    clicking on the LibreOffice logo in the toolbar.
    
    Previously, it was necessary to open the document
    anew.
    
    Change-Id: Ia5fde9b6019251d76d46df97fad2ffd64a0f5a30
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/167721
    Tested-by: Jenkins
    Reviewed-by: Michael Weghorn <m.wegh...@posteo.de>

diff --git 
a/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java 
b/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java
index 1b0b3915c349..315656b6ef7e 100644
--- a/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java
+++ b/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java
@@ -73,7 +73,7 @@ public class LibreOfficeMainActivity extends 
AppCompatActivity implements Settin
 
     private static boolean mIsExperimentalMode;
     private static boolean mIsDeveloperMode;
-    private static boolean mbISReadOnlyMode;
+    private static boolean mbReadOnlyDoc;
 
     private DrawerLayout mDrawerLayout;
     Toolbar toolbarTop;
@@ -168,21 +168,20 @@ public class LibreOfficeMainActivity extends 
AppCompatActivity implements Settin
         // create TextCursorLayer
         mDocumentOverlay = new DocumentOverlay(this, layerView);
 
-        mbISReadOnlyMode = !isExperimentalMode();
+        mbReadOnlyDoc = false;
 
         final Uri docUri = getIntent().getData();
         if (docUri != null) {
             if (docUri.getScheme().equals(ContentResolver.SCHEME_CONTENT)
                     || 
docUri.getScheme().equals(ContentResolver.SCHEME_ANDROID_RESOURCE)) {
-                final boolean isReadOnlyDoc  = (getIntent().getFlags() & 
Intent.FLAG_GRANT_WRITE_URI_PERMISSION) == 0;
-                mbISReadOnlyMode = !isExperimentalMode() || isReadOnlyDoc;
+                mbReadOnlyDoc  = (getIntent().getFlags() & 
Intent.FLAG_GRANT_WRITE_URI_PERMISSION) == 0;
                 Log.d(LOGTAG, "SCHEME_CONTENT: getPath(): " + 
docUri.getPath());
 
                 String displayName = 
FileUtilities.retrieveDisplayNameForDocumentUri(getContentResolver(), docUri);
                 toolbarTop.setTitle(displayName);
 
             } else if (docUri.getScheme().equals(ContentResolver.SCHEME_FILE)) 
{
-                mbISReadOnlyMode = true;
+                mbReadOnlyDoc = true;
                 Log.d(LOGTAG, "SCHEME_FILE: getPath(): " + docUri.getPath());
                 toolbarTop.setTitle(docUri.getLastPathSegment());
             }
@@ -344,7 +343,7 @@ public class LibreOfficeMainActivity extends 
AppCompatActivity implements Settin
 
         String displayName = 
FileUtilities.retrieveDisplayNameForDocumentUri(getContentResolver(), 
mDocumentUri);
         toolbarTop.setTitle(displayName);
-        mbISReadOnlyMode = !isExperimentalMode();
+        mbReadOnlyDoc = false;
         getToolbarController().setupToolbars();
     }
 
@@ -901,7 +900,7 @@ public class LibreOfficeMainActivity extends 
AppCompatActivity implements Settin
     }
 
     public static boolean isReadOnlyMode() {
-        return mbISReadOnlyMode;
+        return !isExperimentalMode() || mbReadOnlyDoc;
     }
 
     public boolean hasLocationForSave() {

Reply via email to