Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d3a86d82 authored by Stefan Niedermann's avatar Stefan Niedermann
Browse files

#26 Implement getCurrentSingleSignOnAccount() livecycle aware

parent f163b632
Loading
Loading
Loading
Loading
+74 −0
Original line number Diff line number Diff line
/*
 * Nextcloud SingleSignOn
 *
 * @author Stefan Niedermann
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.nextcloud.android.sso.helper;

import static com.nextcloud.android.sso.helper.SingleAccountHelper.getCurrentSingleSignOnAccount;

import android.content.Context;
import android.content.SharedPreferences;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;

import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException;
import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException;
import com.nextcloud.android.sso.model.SingleSignOnAccount;

public class CurrentSingleSignOnAccountLiveData extends LiveData<SingleSignOnAccount> {

    private final Context context;
    private final SharedPreferences sharedPrefs;
    private final SharedPreferences.OnSharedPreferenceChangeListener preferenceChangeListener;

    CurrentSingleSignOnAccountLiveData(@NonNull Context context, @NonNull SharedPreferences sharedPrefs, @NonNull String key) {
        this.context = context;
        this.sharedPrefs = sharedPrefs;
        this.preferenceChangeListener = (changedPrefs, changedKey) -> {
            if (key.equals(changedKey)) {
                try {
                    setValue(getValueFromPreferences());
                } catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e) {
                    e.printStackTrace();
                }
            }
        };
    }

    @Override
    protected void onActive() {
        super.onActive();
        try {
            setValue(getValueFromPreferences());
        } catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e) {
            e.printStackTrace();
        }
        sharedPrefs.registerOnSharedPreferenceChangeListener(preferenceChangeListener);
    }

    private SingleSignOnAccount getValueFromPreferences() throws NextcloudFilesAppAccountNotFoundException, NoCurrentAccountSelectedException {
        return getCurrentSingleSignOnAccount(context);
    }

    @Override
    protected void onInactive() {
        sharedPrefs.unregisterOnSharedPreferenceChangeListener(preferenceChangeListener);
        super.onInactive();
    }
}
 No newline at end of file
+14 −2
Original line number Diff line number Diff line
@@ -24,7 +24,9 @@ import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LiveData;

import com.nextcloud.android.sso.AccountImporter;
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException;
@@ -54,6 +56,10 @@ public final class SingleAccountHelper {
        return AccountImporter.getSingleSignOnAccount(context, getCurrentAccountName(context));
    }

    public static LiveData<SingleSignOnAccount> getCurrentSingleSignOnAccount$(@NonNull Context context) {
        return new CurrentSingleSignOnAccountLiveData(context, AccountImporter.getSharedPreferences(context), PREF_CURRENT_ACCOUNT_STRING);
    }

    /**
     * Warning: This call is writing synchronously to the disk.
     * You should use {@link #setCurrentAccountAsync(Context, String)} if possible.
@@ -81,12 +87,18 @@ public final class SingleAccountHelper {
        AccountImporter.authenticateSingleSignAccount(activity, getCurrentSingleSignOnAccount(activity));
    }

    /**
     * For a lifecycle aware implementation see {@link #getCurrentSingleSignOnAccount$(Context)}
     */
    public static void registerSharedPreferenceChangeListener(Context context, 
                                                                SharedPreferences.OnSharedPreferenceChangeListener listener) {
        AccountImporter.getSharedPreferences(context)
                .registerOnSharedPreferenceChangeListener(listener);
    }

    /**
     * For a lifecycle aware implementation see {@link #getCurrentSingleSignOnAccount$(Context)}
     */
    public static void unregisterSharedPreferenceChangeListener(Context context,
                                                                SharedPreferences.OnSharedPreferenceChangeListener listener) {
        AccountImporter.getSharedPreferences(context)
+13 −0
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import com.nextcloud.android.sso.api.NextcloudAPI;
import com.nextcloud.android.sso.exceptions.AccountImportCancelledException;
import com.nextcloud.android.sso.exceptions.AndroidGetAccountsPermissionNotGranted;
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppNotInstalledException;
import com.nextcloud.android.sso.helper.SingleAccountHelper;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@@ -61,6 +62,12 @@ public class MainActivity extends AppCompatActivity {
                e.printStackTrace();
            }
        });

        /*
         * We can also observe the current SingleSignOnAccount (set via SingleAccountHelper) with LiveData
         */
        SingleAccountHelper.getCurrentSingleSignOnAccount$(this)
                .observe(this, ssoAccount -> Log.i(TAG, "New SingleSignOnAccount set: " + ssoAccount.name));
    }

    @Override
@@ -71,6 +78,12 @@ public class MainActivity extends AppCompatActivity {
            AccountImporter.onActivityResult(requestCode, resultCode, data, this, ssoAccount -> {
                Log.i(TAG, "Imported account: " + ssoAccount.name);

                /*
                 * A little helper to store the currently selected account.
                 * We can query this later if we want to keep working with it.
                 */
                SingleAccountHelper.setCurrentAccountAsync(this, ssoAccount.name);

                /* Network requests need to be performed on a background thread */
                executor.submit(() -> {
                    runOnUiThread(() -> ((TextView) findViewById(R.id.result)).setText(R.string.loading));