Skip to content
This repository has been archived by the owner on Nov 24, 2020. It is now read-only.

Commit

Permalink
Force show login activity if not logged in when starting home activity
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrgn committed May 5, 2012
1 parent 32396f9 commit b2b508b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
Expand Up @@ -12,6 +12,7 @@
import android.widget.Button;
import android.widget.Toast;
import ch.hsr.bieridee.android.R;
import ch.hsr.bieridee.android.config.Auth;

/**
* Activity that shows a list of all beers in our database.
Expand All @@ -20,15 +21,10 @@ public class HomeScreenActivity extends Activity {

private static final String LOG_TAG = HomeScreenActivity.class.getName();

/**
* Called when the activity is first created.
*
* @param savedInstanceState If the activity is being re-initialized after previously being shut down then this Bundle contains the
* data it most recently supplied in onSaveInstanceState(Bundle). <b>Note: Otherwise it is null.</b>
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

this.setContentView(R.layout.homescreen);
this.addOnClickListener((Button) findViewById(R.id_dashboardscreen.buttonBeerlist), BeerListActivity.class);

Expand All @@ -44,6 +40,16 @@ public void onClick(View view) {
findViewById(R.id_dashboardscreen.buttonTimeline).setOnClickListener(notYetImplementedListener);
}

@Override
public void onStart() {
super.onStart();
// Only show screen if login information have been set
if (!Auth.dataAvailable()) {
final Intent intent = new Intent(this.getBaseContext(), LoginScreenActivity.class);
startActivity(intent);
}
}

private void addOnClickListener(Button button, final Class<? extends Activity> activityClass) {
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Expand Down
Expand Up @@ -105,8 +105,7 @@ public void onClick(View arg0) {
}

/**
* Sets the autologin checkbox change listener.
*
* Sets the autologin checkbox change listener:
* If autologin checkbox is disabled, settings are cleared.
*/
private void addAutologinListener() {
Expand Down
33 changes: 22 additions & 11 deletions application/src/main/java/ch/hsr/bieridee/android/config/Auth.java
@@ -1,23 +1,25 @@
package ch.hsr.bieridee.android.config;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import ch.hsr.bieridee.android.BierideeApplication;
import ch.hsr.bieridee.android.activities.LoginScreenActivity;

/**
* Class to store authentication information.
*/
public final class Auth {

private static final String PREFS_NAME = "auth";
private static final String USERNAME_KEY = "username";
private static final String PASSWORD_KEY = "password";
private static final String AUTOLOGIN_KEY = "autologin";

/**
* Returns shared preferences.
* @return SharedPreferences instance
*/
private static SharedPreferences getSharedPreferences() {
return BierideeApplication.getAppContext().getSharedPreferences("auth", Context.MODE_PRIVATE);
return BierideeApplication.getAppContext().getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
}

/**
Expand All @@ -33,9 +35,9 @@ private static SharedPreferences.Editor getEditor() {
*/
public static void setAuth(String username, String password, boolean autologin) {
SharedPreferences.Editor editor = getEditor();
editor.putString("username", username);
editor.putString("password", password);
editor.putBoolean("autologin", autologin);
editor.putString(USERNAME_KEY, username);
editor.putString(PASSWORD_KEY, password);
editor.putBoolean(AUTOLOGIN_KEY, autologin);
editor.commit();
}

Expand All @@ -62,7 +64,7 @@ public static void clearAuth() {
*/
public static String getUsername() {
SharedPreferences authStore = getSharedPreferences();
return authStore.getString("username", "");
return authStore.getString(USERNAME_KEY, "");
}

/**
Expand All @@ -72,15 +74,24 @@ public static String getUsername() {
*/
public static String getPassword() {
SharedPreferences authStore = getSharedPreferences();
return authStore.getString("password", "");
return authStore.getString(PASSWORD_KEY, "");
}

/**
* Returns the autologin setting.
* @return Whether or not autologin is enabled.
*/
public static Boolean getAutologin() {
public static boolean getAutologin() {
SharedPreferences authStore = getSharedPreferences();
return authStore.getBoolean(AUTOLOGIN_KEY, false);
}

/**
* Returns whether or not username and password are available.
* @return
*/
public static boolean dataAvailable() {
SharedPreferences authStore = getSharedPreferences();
return authStore.getBoolean("autologin", false);
return authStore.contains(USERNAME_KEY) && authStore.contains(PASSWORD_KEY);
}
}

0 comments on commit b2b508b

Please sign in to comment.