Skip to content

Conversation

@ezaquarii
Copy link
Collaborator

@ezaquarii ezaquarii commented Nov 4, 2019

  1. eliminate access to shared preferences when setting dark theme.
  2. extend AppPreferences interface to allow subscription for preference changes
  3. improve preferences interface documentation

@AndyScherzinger @tobiasKaminsky @dan0xii

This is my initial proposal to deal with AppPreferences interface bypass.
I tried to address access to AppPreferencesImpl but it looks like we still have
many points where we do cross-cutting calls to private implementation for various
reasons (dodgy workarounds, circular dependencies, etc). Despite my tries, I conclude
that closing AppPreferencesImpl is a bit too much work for a single change and I exhausted
my bandwidth for today.

This PR will address only theme related concerns. I'll follow up with improvements around
*Impl access as my time allows.

Fixes #4791

Signed-off-by: Chris Narkiewicz hello@ezaquarii.com

@ezaquarii ezaquarii force-pushed the ezaquarii/replace-shared-preferences-with-app-preferences-in-base-activity branch from 9de6bc1 to dc32b64 Compare November 4, 2019 20:30
@nextcloud nextcloud deleted a comment Nov 4, 2019
@ezaquarii ezaquarii force-pushed the ezaquarii/replace-shared-preferences-with-app-preferences-in-base-activity branch from dc32b64 to 6a5928c Compare November 4, 2019 20:34
@nextcloud nextcloud deleted a comment Nov 4, 2019
@ezaquarii ezaquarii force-pushed the ezaquarii/replace-shared-preferences-with-app-preferences-in-base-activity branch from 6a5928c to fa4f166 Compare November 4, 2019 20:49
@nextcloud nextcloud deleted a comment Nov 4, 2019
@codecov
Copy link

codecov bot commented Nov 4, 2019

Codecov Report

Merging #4797 into master will increase coverage by 0.1%.
The diff coverage is 70.83%.

@@             Coverage Diff             @@
##             master    #4797     +/-   ##
===========================================
+ Coverage     17.48%   17.58%   +0.1%     
  Complexity        3        3             
===========================================
  Files           376      377      +1     
  Lines         32385    32413     +28     
  Branches       4569     4573      +4     
===========================================
+ Hits           5661     5699     +38     
+ Misses        25813    25798     -15     
- Partials        911      916      +5
Impacted Files Coverage Δ Complexity Δ
...m/nextcloud/client/preferences/AppPreferences.java 0% <0%> (ø) 0 <0> (?)
.../android/ui/activity/ThemedPreferenceActivity.java 66.66% <44.44%> (+1.66%) 0 <0> (ø) ⬇️
...owncloud/android/ui/activity/SettingsActivity.java 38.7% <50%> (+0.04%) 0 <0> (ø) ⬇️
...com/owncloud/android/ui/activity/BaseActivity.java 35.55% <57.14%> (ø) 0 <0> (ø) ⬇️
...xtcloud/client/preferences/AppPreferencesImpl.java 58.6% <92%> (+4.89%) 0 <0> (ø) ⬇️
...ncloud/android/ui/fragment/OCFileListFragment.java 25.57% <0%> (-0.41%) 0% <0%> (ø)
...cloud/android/ui/activity/FileDisplayActivity.java 19.93% <0%> (-0.25%) 0% <0%> (ø)
...in/java/com/owncloud/android/utils/ThemeUtils.java 51.57% <0%> (+1.96%) 0% <0%> (ø) ⬇️
...m/owncloud/android/ui/activity/DrawerActivity.java 45.19% <0%> (+2.29%) 0% <0%> (ø) ⬇️

@AndyScherzinger
Copy link
Member

In general it looks good while there are now two keys for the theme which should imho be just one.

@ezaquarii ezaquarii force-pushed the ezaquarii/replace-shared-preferences-with-app-preferences-in-base-activity branch from fa4f166 to 3961fd6 Compare November 4, 2019 21:12
@nextcloud nextcloud deleted a comment Nov 4, 2019
@nextcloud-android-bot
Copy link
Collaborator

@ezaquarii
Copy link
Collaborator Author

ezaquarii commented Nov 4, 2019

In general it looks good while there are now two keys for the theme which should imho be just one.

Yeah, I just checked the settings XML and it looks like Android is putting the UI key into the store,
which is not good. I'll roll this back as I have no good solution for this atm.

The rationale behind this experiment is that conceptually we have 1 key somewhere behind AppPreferences and nobody should touch it in any way other than calling strongly typed API. This is on purpose:

  1. people should not liberally write into the SharedPreferences bypassing the interface we provide, with all sort of potential sanitation and safeguards; we'd like to discoruage people from fiddling with the store on their own, so we can safely evolve this area in the future.
  2. UI should not be concerned how or where we store app settings - we might find that JSON stored on Nextcloud server is the way to go at some time and I'd argue that this should be the goal eventually - I have some vested interest here, as I'm kinda tired of reconfiguring my autoupload each time I reinstall the app (;
  3. tracking preferences access via interface is as easy as tracking a method call; there is is one entry gate and no surprises

Currently keys must be maintained in sync in 3 distinct places:

  1. AppPreferencesImpl - to access the store
  2. settings.xml - so the UI has backdoor into the store layer
  3. SettingsActivity - so we can access UI controls

BTW, those sets are not 100% overlapping.

Now, you could argue that we can just move those keys to AppPreferences interface to unify 1. and 2., but my fear is that we again start seeing stuff like this:

void outOfTheBlue() {
    SharedPreferences p = MainApp.getInstance().getSharedPreferences(); // circular deps again
    p.edit().setString(AppPrefernces.PREFS__DODGY_KEY, "magic value").apply(); // out of band calls

    MainApp.getInstance().getSharedPreferences().setBoolean(AppPrefernces.PREFS__WORKAROUND_FLAG, !MainApp.getInstance().getSharedPreferences().setBoolean(AppPrefernces.PREFS__WORKAROUND_FLAG, false)).commit();
}

and we just got rid of it recently. Those will slip through code reviews even if we stay alert.

Enough ranting. 🙃 Having said that, I'm reverting my experiment as I have no good and quick workaround for antipattern of the native Android API and the solution we have right now seems work well enough. Not great - not terrible. ¯\_(ツ)_/¯

@ezaquarii ezaquarii force-pushed the ezaquarii/replace-shared-preferences-with-app-preferences-in-base-activity branch from 3961fd6 to f8e1de0 Compare November 4, 2019 23:33
- Eliminate access to shared preferences when setting dark theme.
- Extend AppPreferences interface to allow subscription for preference changes
- Improve preferences interface documentation

Fixes #4791

Signed-off-by: Chris Narkiewicz <hello@ezaquarii.com>
@ezaquarii ezaquarii force-pushed the ezaquarii/replace-shared-preferences-with-app-preferences-in-base-activity branch from f8e1de0 to 6b01382 Compare November 5, 2019 06:01
@nextcloud-android-bot
Copy link
Collaborator

APK file: https://www.kaminsky.me/nc-dev/android-artifacts/11544.apk

qrcode

To test this change/fix you can simply download above APK file and install and test it in parallel to your existing Nextcloud app.

@nextcloud-android-bot
Copy link
Collaborator

Codacy

304

Lint

TypemasterPR
Warnings5959
Errors00

SpotBugs (new)

Warning TypeNumber
Bad practice Warnings26
Correctness Warnings69
Internationalization Warnings13
Malicious code vulnerability Warnings5
Multithreaded correctness Warnings9
Performance Warnings119
Security Warnings44
Dodgy code Warnings138
Total423

SpotBugs (master)

Warning TypeNumber
Bad practice Warnings26
Correctness Warnings69
Internationalization Warnings13
Malicious code vulnerability Warnings5
Multithreaded correctness Warnings9
Performance Warnings119
Security Warnings44
Dodgy code Warnings138
Total423

private static final String PREF__FOLDER_SORT_ORDER = "folder_sort_order";
private static final String PREF__FOLDER_LAYOUT = "folder_layout";
public static final String PREF__THEME = "darkTheme";
static final String PREF__DARK_THEME_ENABLED = "dark_theme_enabled";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will likely chance when @tobiasKaminsky pushes his PR to the repo adding a 3rd option for the theme values (dark, light, system)

Copy link
Collaborator Author

@ezaquarii ezaquarii Nov 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be interesting. I wild guess that the change will be based on the SharedPreferences code that I just removed and the preference will become some kind of enum instead of boolean?

I'm fine with resolving conflicts on my side, let's just make sure we don't have to do a complete rewrite. (-:

Is there any public branch I could potentially try?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any public branch I could potentially try?

Don't know, I just saw this earlier this evening, which is why I mentioned @tobiasKaminsky here --> #4757 (comment)

@AndyScherzinger
Copy link
Member

Fine with me. Just one minor comment that this PR and a soon to be openend PR by @tobiasKaminsky will have conflicts while both PRs will change the type of the preference and thus (fine since master is in alpha state for v3.10) both PRs will break the activated dark theme (since the key changed twice then)

@tobiasKaminsky
Copy link
Member

We merge this one, and then I change it within my upcoming PR.
Sometimes it will just happen that we create conflicts…

@tobiasKaminsky tobiasKaminsky merged commit 890789f into master Nov 6, 2019
@delete-merged-branch delete-merged-branch bot deleted the ezaquarii/replace-shared-preferences-with-app-preferences-in-base-activity branch November 6, 2019 12:52
@AndyScherzinger AndyScherzinger added this to the Nextcloud App 3.10.0 milestone Nov 6, 2019
tobiasKaminsky added a commit that referenced this pull request Nov 7, 2019
890789f Merge pull request #4797 from nextcloud/ezaquarii/replace-shared-preferences-with-app-preferences-in-base-activity
180bdee Merge pull request #4801 from Infomaniak/feature/resolve-set-as-wallpaper-issue
039adc0 [tx-robot] updated from transifex
b59938d daily dev 20191106
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bypassing AppPreferences in BaseActivity

5 participants