Skip to content

Commit

Permalink
Account management (#48)
Browse files Browse the repository at this point in the history
* Merge Jeffs changes
* bump to new versions
* first working implementation of Account management, closes #10
* fix #29
  • Loading branch information
ramack authored Oct 4, 2018
1 parent a551814 commit 330b65a
Show file tree
Hide file tree
Showing 73 changed files with 1,458 additions and 259 deletions.
18 changes: 18 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

![](https://travis-ci.org/Piwigo/Piwigo-Android.svg)

Currently under development by [@Philio](https://github.com/Philio), please get in touch if you're interested in contributing to
Currently under development by the Piwigo-Mobile team, please get in touch if you're interested in contributing to
the project.

If you're considering contributing, a working knowledge of the following would be extremely useful:
Expand Down Expand Up @@ -33,4 +33,4 @@ If you're considering contributing, a working knowledge of the following would b

## Licensing
The project is delivered under the GPL in version 3, or (at your option) any later version of that license. By submitting code to the project by patches, pull request or any other form the authors guarantee that it is either their own work and they accept to distribute their content under this license or the contribution is (re-)distributable under the terms of this license. If not specifically marked with a dedicated copyright notice, the copyright of the source code is transfered to to Piwigo project as far as permitted by applicable law. If necessary for legal reasons the source code author(s) shall be extracted from the meta data of the version control system.
Copyright notices in this project are mostly given with a reference to the year in form of xxxx-yyyy as a short cut with the meaning, that every year in the given range is a copyrigthable year.
Copyright notices in this project are mostly given with a reference to the year in form of xxxx-yyyy as a short cut with the meaning, that every year in the given range is a copyrightable year.
40 changes: 17 additions & 23 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
apply plugin: 'com.android.application'
apply plugin: 'com.fernandocejas.frodo'
apply plugin: 'io.fabric'

def isCi = "true".equals(System.getenv("CI"))
def preDexEnabled = "true".equals(System.getProperty("pre-dex", "true"))
def isCi = "true" == System.getenv("CI")
def preDexEnabled = "true" == System.getProperty("pre-dex", "true")

android {
compileSdkVersion 26
buildToolsVersion '26.0.2'
compileSdkVersion 28

defaultConfig {
applicationId "org.piwigo"
minSdkVersion 14
targetSdkVersion 26
targetSdkVersion 28
versionCode 1
versionName "2.0"
versionName "0.4"
}
buildTypes {
debug {
Expand Down Expand Up @@ -56,11 +54,11 @@ android {
}

ext {
supportVersion = '26.1.0'
lifecycleVersion = '1.0.0-rc1'
daggerVersion = '2.11'
okhttpVersion = '3.9.0'
retrofitVersion = '2.3.0'
supportVersion = '28.0.0'
lifecycleVersion = '1.1.1'
daggerVersion = '2.15'
okhttpVersion = '3.11.0'
retrofitVersion = '2.4.0'
assertjVersion = '1.2.0'
}

Expand All @@ -86,17 +84,13 @@ dependencies {
implementation 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0'
implementation 'io.reactivex:rxjava:1.3.2'
implementation 'io.reactivex:rxandroid:1.2.1'
googlePlayImplementation('com.crashlytics.sdk.android:crashlytics:2.5.2@aar') {
transitive = true
}
implementation 'com.github.jorgecastilloprz:fabprogresscircle:1.01@aar'
implementation 'com.google.guava:guava:23.2-android'
annotationProcessor 'com.google.guava:guava:23.2-jre'
implementation 'org.apache.commons:commons-lang3:3.4'
implementation 'com.google.guava:guava:24.1-jre'
annotationProcessor 'com.google.guava:guava:24.1-jre'
implementation 'org.apache.commons:commons-lang3:3.8.1'


testImplementation 'junit:junit:4.12'
testImplementation "org.robolectric:robolectric:3.4.2"
testImplementation "org.robolectric:robolectric:3.6.1"
testImplementation("com.squareup.assertj:assertj-android:${assertjVersion}") {
exclude group: 'com.android.support'
}
Expand All @@ -108,8 +102,8 @@ dependencies {
}
testAnnotationProcessor "com.google.dagger:dagger-compiler:${daggerVersion}"
testImplementation "android.arch.core:core-testing:${lifecycleVersion}"
testImplementation 'org.mockito:mockito-core:2.10.0'
testImplementation 'com.google.guava:guava:23.2-android'
testAnnotationProcessor 'com.google.guava:guava:23.2-jre'
testImplementation 'org.mockito:mockito-core:2.13.0'
testImplementation 'com.google.guava:guava:24.1-jre'
testAnnotationProcessor 'com.google.guava:guava:24.1-jre'
testImplementation 'com.google.code.findbugs:jsr305:3.0.1'
}
5 changes: 5 additions & 0 deletions app/src/debug/res/values/constants.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<resources>

<string name="account_type">org.piwigo.account_debug</string>

</resources>
1 change: 1 addition & 0 deletions app/src/floss/java/io/fabric/sdk/android/Fabric.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import android.content.Context;

@SuppressWarnings("EmptyMethod")
public class Fabric {

public static void with(Context context, Object... kits) {
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
android:name="android.permission.GET_ACCOUNTS"
android:maxSdkVersion="22" />

<uses-permission
android:name="android.permission.MANAGE_ACCOUNTS"
android:maxSdkVersion="22" />

<application
android:name=".PiwigoApplication"
android:allowBackup="true"
Expand All @@ -35,6 +39,11 @@
android:name=".ui.main.MainActivity"
android:label="@string/title_activity_main"
android:theme="@style/Theme.Piwigo.NavDrawer" />
<activity
android:name=".ui.account.ManageAccountsActivity"
android:label="@string/title_activity_accounts"
android:theme="@style/Theme.Piwigo.NavDrawer"
android:parentActivityName=".ui.main.MainActivity" />

<service android:name=".accounts.AuthenticatorService">
<intent-filter>
Expand Down
94 changes: 77 additions & 17 deletions app/src/main/java/org/piwigo/accounts/UserManager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Piwigo for Android
* Copyright (C) 2016-2017 Piwigo Team http://piwigo.org
* Copyright (C) 2016-2018 Piwigo Team http://piwigo.org
* Copyright (C) 2018 Raphael Mack
*
* 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
Expand All @@ -20,18 +21,22 @@

import android.accounts.Account;
import android.accounts.AccountManager;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;

import org.apache.commons.lang3.StringUtils;
import org.piwigo.R;
import org.piwigo.io.repository.PreferencesRepository;

import java.util.List;

public class UserManager {

@VisibleForTesting static final String KEY_IS_GUEST = "is_guest";
Expand All @@ -46,10 +51,28 @@ public class UserManager {
private final Resources resources;
private final PreferencesRepository preferencesRepository;

private final MutableLiveData<Account> mCurrentAccount;
private final MutableLiveData<List<Account>> mAllAccounts;

public UserManager(AccountManager accountManager, Resources resources, PreferencesRepository preferencesRepository) {
this.accountManager = accountManager;
this.resources = resources;
this.preferencesRepository = preferencesRepository;
this.mCurrentAccount = new MutableLiveData<>();
this.mAllAccounts = new MutableLiveData<>();

refreshAccounts();
setActiveAccount(preferencesRepository.getActiveAccountName());
}

/* refresh account list - to be called by activities which are aware
* of a change in the accounts */
public void refreshAccounts() {
Account[] accounts = accountManager.getAccountsByType(resources.getString(R.string.account_type));
mAllAccounts.setValue(ImmutableList.copyOf(accounts));

Account a = mCurrentAccount.getValue();
setActiveAccount(a == null ? "" : a.name);
}

public boolean isLoggedIn() {
Expand All @@ -74,21 +97,13 @@ public Account createUser(String siteUrl, String username, String password, Stri
}
}

@SuppressWarnings("Guava")
public Optional<Account> getActiveAccount() {
Account[] accounts = accountManager.getAccountsByType(resources.getString(R.string.account_type));
if (accounts.length == 0) {
return Optional.absent();
}
String activeAccount = preferencesRepository.getActiveAccount();
if (!TextUtils.isEmpty(activeAccount)) {
for (Account account : accounts) {
if (account.name.equals(activeAccount)) {
return Optional.of(account);
}
}
}
return Optional.of(accounts[0]);
/* observe this LiveData for notifications on account switches */
public LiveData<Account> getActiveAccount() {
return mCurrentAccount;
}

public LiveData<List<Account>> getAccounts(){
return mAllAccounts;
}

public String getSiteUrl(Account account) {
Expand All @@ -107,6 +122,14 @@ public String getToken(Account account) {
return accountManager.getUserData(account, KEY_TOKEN);
}

public boolean isGuest(Account account) {
return Boolean.parseBoolean(accountManager.getUserData(account, KEY_IS_GUEST));
}

public String getAccountName(Account account){
return getAccountName(getSiteUrl(account), getUsername(account));
}

private String getAccountName(String siteUrl, String username) {
Uri uri = Uri.parse(siteUrl);
String sitename = uri.getHost() + uri.getPath();
Expand Down Expand Up @@ -139,4 +162,41 @@ private Account createGuestUser(String siteUrl) {
accountManager.addAccountExplicitly(account, null, userdata);
return account;
}

public Account getAccountWithName(String accountName){
Account[] accounts = accountManager.getAccountsByType(resources.getString(R.string.account_type));

for (Account account : accounts) {
if (account.name.equals(accountName)) {
return account;
}
}
return null;
}

public void setActiveAccount(Account activeAccount) {
mCurrentAccount.setValue(activeAccount);
}

public void setActiveAccount(String activeAccount) {
Account[] accounts = accountManager.getAccountsByType(resources.getString(R.string.account_type));

if (!TextUtils.isEmpty(activeAccount)) {
for (Account account : accounts) {
if (account.name.equals(activeAccount)) {
preferencesRepository.setActiveAccount(activeAccount);
mCurrentAccount.setValue(account);
return;
}
}
}

/* the selected account is not available select default */
if(accounts.length > 0) {
mCurrentAccount.setValue(accounts[0]);
}else{
mCurrentAccount.setValue(null);
}
}

}
Loading

0 comments on commit 330b65a

Please sign in to comment.