Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revised Issue #9 Added Account Edit Fragment and use POJO in controller #37

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package="org.hisp.dhis.android.dashboard.api">

<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application android:allowBackup="true" />

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import org.hisp.dhis.android.dashboard.api.persistence.preferences.DateTimeManager;
import org.hisp.dhis.android.dashboard.api.persistence.preferences.LastUpdatedManager;

import retrofit.client.Response;

import static org.hisp.dhis.android.dashboard.api.utils.Preconditions.isNull;

public class DhisController {
Expand Down Expand Up @@ -99,6 +101,14 @@ private UserAccount signInUser(HttpUrl serverUrl, Credentials credentials) throw
return user;
}

public Response setUserProfileDetails(HttpUrl serverUrl
, Credentials credentials, UserAccount userAccount) throws APIException {
DhisApi dhisApi = RepoManager
.createService(serverUrl, credentials);
return new UserController(dhisApi)
.updateProfileDetails(userAccount);
}

public void invalidateSession() {
LastUpdatedManager.getInstance().invalidate();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@
import java.util.HashMap;
import java.util.Map;

import retrofit.client.Response;

/**
* @author Araz Abishov <araz.abishov.gsoc@gmail.com>.
*/
final class UserController {
private final DhisApi mDhisApi;

public UserController(DhisApi dhisApi) {
mDhisApi = dhisApi;
}
Expand Down Expand Up @@ -112,4 +113,12 @@ public UserAccount updateUserAccount() throws APIException {
userAccount.save();
return userAccount;
}

public Response updateProfileDetails(UserAccount userAccount) throws APIException {
Response response = mDhisApi.postUserAccount(userAccount);
if(response.getStatus()==200)
userAccount.save();
return response;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public interface DhisApi {
@GET("/me/")
UserAccount getCurrentUserAccount(@QueryMap Map<String, String> queryParams);

@POST("/me/user-account/")
Response postUserAccount(@Body UserAccount userAccount);

/////////////////////////////////////////////////////////////////////////
// Methods for getting Dashboard and DashboardItems
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public DateTime getLastUpdated(ResourceType type) {
* Removes all key-value pairs.
*/
public void delete() {
mPrefs.edit().clear().commit();
mPrefs.edit().clear().apply();
}

public void deleteLastUpdated(ResourceType type) {
Expand All @@ -63,15 +63,15 @@ public boolean isLastUpdatedSet(ResourceType type) {
}

private void putString(String key, String value) {
mPrefs.edit().putString(key, value).commit();
mPrefs.edit().putString(key, value).apply();
}

private String getString(String key) {
return mPrefs.getString(key, null);
}

private void deleteString(String key) {
mPrefs.edit().remove(key).commit();
mPrefs.edit().remove(key).apply();
}

}
14 changes: 14 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
xmlns:android="http://schemas.android.com/apk/res/android"
package="org.hisp.dhis.android.dashboard">

<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS"/>

<application
android:name="org.hisp.dhis.android.dashboard.DhisApplication"
android:allowBackup="true"
Expand All @@ -14,6 +19,15 @@
android:name="org.hisp.dhis.android.dashboard.DhisService"
android:exported="false" />

<service android:name="org.hisp.dhis.android.dashboard.AccountAuthenticatorService"
android:exported="false">
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator" />
</intent-filter>
<meta-data android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/authenticator" />
</service>

<activity
android:name="org.hisp.dhis.android.dashboard.ui.activities.LoginActivity"
android:label="@string/title_activity_login"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package org.hisp.dhis.android.dashboard;

import android.accounts.AbstractAccountAuthenticator;
import android.accounts.Account;
import android.accounts.AccountAuthenticatorResponse;
import android.accounts.AccountManager;
import android.accounts.NetworkErrorException;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import org.hisp.dhis.android.dashboard.ui.activities.LoginActivity;
import org.hisp.dhis.android.dashboard.utils.AccountUtils;

import static android.accounts.AccountManager.KEY_BOOLEAN_RESULT;

public class AccountAuthenticator extends AbstractAccountAuthenticator {

private Context mContext;

public AccountAuthenticator(Context context) {
super(context);
this.mContext = context;
}

@Override
public Bundle addAccount(AccountAuthenticatorResponse response, String accountType
, String authTokenType, String[] requiredFeatures, Bundle options)
throws NetworkErrorException {
final Intent intent = new Intent(mContext, LoginActivity.class);
intent.putExtra(LoginActivity.ARG_ACCOUNT_TYPE, accountType);
intent.putExtra(LoginActivity.ARG_AUTH_TYPE, authTokenType);
intent.putExtra(LoginActivity.ARG_IS_ADDING_NEW_ACCOUNT, true);
intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);

final Bundle bundle = new Bundle();
bundle.putParcelable(AccountManager.KEY_INTENT, intent);
return bundle;
}

@Override
public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
return null;
}

@Override
public Bundle confirmCredentials(AccountAuthenticatorResponse response
, Account account, Bundle options) throws NetworkErrorException {
return null;
}

@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response
, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
if (!authTokenType.equals(AccountUtils.AUTHTOKEN_TYPE_READ_ONLY)
&& !authTokenType.equals(AccountUtils.AUTHTOKEN_TYPE_FULL_ACCESS)) {
final Bundle result = new Bundle();
result.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authTokenType");
return result;
}

final AccountManager accountManager = AccountManager.get(mContext);
String authToken = accountManager.peekAuthToken(account, authTokenType);

if (!TextUtils.isEmpty(authToken)) {
final Bundle result = new Bundle();
result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
result.putString(AccountManager.KEY_AUTHTOKEN, authToken);
return result;
}

final Intent intent = new Intent(mContext, LoginActivity.class);
intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
intent.putExtra(LoginActivity.ARG_ACCOUNT_TYPE, account.type);
intent.putExtra(LoginActivity.ARG_AUTH_TYPE, authTokenType);
intent.putExtra(LoginActivity.ARG_ACCOUNT_NAME, account.name);
final Bundle bundle = new Bundle();
bundle.putParcelable(AccountManager.KEY_INTENT, intent);
return bundle;
}

@Override
public String getAuthTokenLabel(String authTokenType) {
if (AccountUtils.AUTHTOKEN_TYPE_FULL_ACCESS.equals(authTokenType))
return AccountUtils.AUTHTOKEN_TYPE_FULL_ACCESS_LABEL;
else if (AccountUtils.AUTHTOKEN_TYPE_READ_ONLY.equals(authTokenType))
return AccountUtils.AUTHTOKEN_TYPE_READ_ONLY_LABEL;
else
return authTokenType + " (Label)";
}

@Override
public Bundle updateCredentials(AccountAuthenticatorResponse response
, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
return null;
}

@Override
public Bundle hasFeatures(AccountAuthenticatorResponse response
, Account account, String[] features) throws NetworkErrorException {
final Bundle result = new Bundle();
result.putBoolean(KEY_BOOLEAN_RESULT, false);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.hisp.dhis.android.dashboard;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class AccountAuthenticatorService extends Service {
@Override
public IBinder onBind(Intent intent) {
AccountAuthenticator authenticator = new AccountAuthenticator(this);
return authenticator.getIBinder();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@
import org.hisp.dhis.android.dashboard.api.utils.EventBusProvider;
import org.hisp.dhis.android.dashboard.ui.events.UiEvent;

import retrofit.client.Response;

/**
* @author Araz Abishov <araz.abishov.gsoc@gmail.com>.
*/
public final class DhisService extends Service {
public static final int LOG_IN = 1;
public static final int CONFIRM_USER = 2;
public static final int LOG_OUT = 3;
public static final int EDIT_PROFILE = 4;
public static final int SYNC_DASHBOARDS = 5;
public static final int SYNC_DASHBOARD_CONTENT = 6;
public static final int SYNC_INTERPRETATIONS = 7;
Expand Down Expand Up @@ -105,7 +108,16 @@ public void onFinish(UiEvent result) {
}
});
}

public void editProfileDetails(final HttpUrl serverUrl, final Credentials credentials
,final UserAccount userAccount) {
JobExecutor.enqueueJob(new NetworkJob<Response>(EDIT_PROFILE,
ResourceType.USERS) {
@Override
public Response execute() throws APIException {
return mDhisController.setUserProfileDetails(serverUrl, credentials, userAccount);
}
});
}
public void confirmUser(final Credentials credentials) {
JobExecutor.enqueueJob(new NetworkJob<UserAccount>(CONFIRM_USER,
ResourceType.USERS) {
Expand Down
Loading