/*
********************* [ P O C K E T C A M P U S ] *****************
* [ LICENCE ] see "licence"-file in the root directory
* [ MAINTAINER ] jonathan.baeriswyl@epfl.ch
* [ STATUS ] usable
*
**************************[ C O M M E N T S ]**********************
*
* A simple preferences gui
*
*******************************************************************
*/
package org.pocketcampus.gui.preferences;
import org.pocketcampus.R;
import org.pocketcampus.gui.mainscreen.HomeScreenSwitcher;
import org.pocketcampus.gui.mainscreen.InternetUtilities;
import org.pocketcampus.gui.mainscreen.InternetUtilities.ConnexionStatus;
import org.pocketcampus.gui.social.FriendsList;
import org.pocketcampus.gui.social.NotConnectedAlert;
import org.pocketcampus.gui.social.PositionUpdaterService;
import org.pocketcampus.gui.social.SocialCreateAccount;
import org.pocketcampus.gui.social.SocialLogin;
import org.pocketcampus.serverapi.ServerAPI;
import org.pocketcampus.shared.accounts.AuthToken;
import org.pocketcampus.shared.accounts.SessionId;
import org.pocketcampus.shared.accounts.Username;
import org.pocketcampus.shared.exceptions.ServerException;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.Preference.OnPreferenceClickListener;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
public class Preferences extends PreferenceActivity implements
OnItemSelectedListener {
int homeScr;
int oldHomeScr;
private SharedPreferences sharedPreferences_;
private Preferences thisActivity_;
private String username_;
private String sessionId_;
private boolean logged_;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(R.string.Activity_Names_Preferences);
sharedPreferences_ = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String status = sharedPreferences_.getString("preferences_keyStatus", "N/a");
username_ = sharedPreferences_.getString("preferences_keyUsername", "N/a");
sessionId_ = sharedPreferences_.getString("preferences_keySessionId", "N/a");
thisActivity_ = this;
//if we can authenticate the session, then we must adapt the layout
try {
if(
!(new InternetUtilities()).checkInternetStatus(thisActivity_, false).equals(ConnexionStatus.INTERNET_CONNEXION_UNAVAILABLE) && //internet available
(!username_.equals("N/a") && !sessionId_.equals("N/a")) && //session data not null
(new ServerAPI()).authenticate(new AuthToken(new Username(username_), new SessionId(sessionId_)))//session active
) {
addPreferencesFromResource(R.xml.preferences_logged);
thisActivity_.findPreference("socialPreferencesUsernameCategory").setTitle(username_);
logged_ = true;
} else {
addPreferencesFromResource(R.xml.preferences);
logged_ = false;
}
} catch(ServerException e) {
e.printStackTrace();
addPreferencesFromResource(R.xml.preferences);
logged_ = false;
}
sharedPreferences_
.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(
SharedPreferences sharedPreferences, String key) {
if (key.equals("preferences_keyView")) {
Intent homeActivity = new Intent(
Preferences.this,
HomeScreenSwitcher.class);
startActivity(homeActivity);
finish();
}
}
});
String status1 = sharedPreferences_.getString("preferences_keyStatus",
"N/a");
// System.out.println(status1 + " " + status);
Preference homescreenPref = (Preference) findPreference("preferences_homescreen");
homescreenPref
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
Intent homeActivity = new Intent(
Preferences.this, UICarousel.class);
startActivity(homeActivity);
finish();
return true;
}
});
Preference backgroundPref = (Preference) findPreference("preferences_background");
backgroundPref
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
Intent backgroundActivity = new Intent(
Preferences.this, BackgroundCarousel.class);
startActivity(backgroundActivity);
finish();
return true;
}
});
if(!logged_) {
//listener for login button
Preference preferencesLogin = (Preference) findPreference("preferences_login");
preferencesLogin.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
Intent intent = new Intent(Preferences.this, SocialLogin.class);
startActivity(intent);
Intent refresh = getIntent();
thisActivity_.startActivity(refresh);
finish();
return true;
}
});
//account creating button
Preference preferencesCreateAccount = (Preference) findPreference("preferences_create");
preferencesCreateAccount.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
Intent intent = new Intent(Preferences.this, SocialCreateAccount.class);
startActivity(intent);
return true;
}
});
} else {
//logout button
Preference preferencesLogout = (Preference) findPreference("preferences_logout");
preferencesLogout.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
try {
(new ServerAPI()).logout(new AuthToken(new Username(username_), new SessionId(sessionId_)));
sharedPreferences_.edit()
.putString("preferences_keyUsername", "N/a")
.putString("preferences_keySessionId", "N/a")
.commit();
stopService(new Intent(thisActivity_, PositionUpdaterService.class));
} catch(ServerException e) {
e.printStackTrace();
new NotConnectedAlert(thisActivity_, thisActivity_.getString(R.string.social_login));
}
Intent intent = new Intent(Preferences.this, HomeScreenSwitcher.class);
startActivity(intent);
finish();
return true;
}
});
//friends button
Preference preferencesFriends = (Preference) findPreference("preferences_friends");
preferencesFriends.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
Intent intent = new Intent(Preferences.this, FriendsList.class);
startActivity(intent);
return true;
}
});
}
}
public void onBackPressed() {
Intent homeActivity = new Intent(Preferences.this,
HomeScreenSwitcher.class);
startActivity(homeActivity);
this.finish();
}
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
}
public void onNothingSelected(AdapterView<?> arg0) {
}
static public void prefScreenChanged(int homeScr) {
}
}
|