package se.dat255.grupp12; import android.accounts.Account; import android.accounts.AccountManager; import android.accounts.AccountManagerCallback; import android.accounts.AccountManagerFuture; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.util.Patterns; import android.view.MenuItem; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; import android.view.*; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.regex.Pattern; import retrofit.Callback; import retrofit.RetrofitError; import retrofit.client.Response; /** * Created by Oscar on 2013-10-01. */ public class LoginActivity extends Activity { private AccountManager mAccountManager; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().requestFeature(Window.FEATURE_ACTION_BAR); getActionBar().hide(); setContentView(R.layout.login_view); ImageView logo = (ImageView) findViewById(R.id.loginLogo); logo.setImageResource(R.drawable.logo_large); FileHandler fileHandler = new FileHandler(); try { fileHandler.readAddressFromFile(this); } catch (IOException e) { e.printStackTrace(); } } /** * onClick method for logging in the user to the server * @param view */ public void clickedLogin(View view) { final EditText input = new EditText(this); input.setText(ServerConnection.getAddress()); final LoginActivity thisActivity = this; //Because "this not applicable in callbacks from retroFit" new AlertDialog.Builder(this) .setTitle("Server-IP") .setMessage("Skriv in serverns IP") .setView(input) .setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { ServerConnection.setAddress(input.getText().toString()); Toast.makeText(getApplicationContext(), input.getText().toString() + " är nu serverns adress.", Toast.LENGTH_SHORT).show(); mAccountManager = AccountManager.get(LoginActivity.this); final Account[] accounts = mAccountManager.getAccountsByType("com.google"); if (accounts.length == 0) { Toast.makeText(getApplicationContext(), "No accounts found!", Toast.LENGTH_SHORT).show(); } else { Bundle options = new Bundle(); mAccountManager.getAuthToken( accounts[0], "oauth2:https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email", options, thisActivity, new AccountManagerCallback<Bundle>() { @Override public void run(AccountManagerFuture<Bundle> result) { Bundle bundle = null; try { bundle = result.getResult(); final String mail = bundle.getString(AccountManager.KEY_ACCOUNT_NAME); String token = bundle.getString(AccountManager.KEY_AUTHTOKEN); Intent launch = (Intent) result.getResult().get(AccountManager.KEY_INTENT); if (launch != null) { startActivityForResult(launch, 0); return; } Toast.makeText(getApplicationContext(), "Ansluter...", Toast.LENGTH_SHORT).show(); //ServerConnection.setAddress("192.168.0.196"); ServerConnection.server().authenticateUserAsync(mail,token,new Callback<String>() { @Override public void success(String s, Response response) { Toast.makeText(getApplicationContext(), "Success!", Toast.LENGTH_SHORT).show(); User.setUserEmails(Arrays.asList(mail)); Intent loginUser; loginUser = new Intent(thisActivity, MainActivity.class); startActivity(loginUser); ServerConnection.setConnectedToServer(true); } @Override public void failure(RetrofitError retrofitError) { retrofitError.printStackTrace(); Toast.makeText(getApplicationContext(), retrofitError.getMessage(), Toast.LENGTH_SHORT).show(); ServerConnection.setConnectedToServer(false); } }); } catch (Exception e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), "Exception e", Toast.LENGTH_SHORT).show(); } } }, null ); } } }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // Do nothing. } }).show(); } /** * onClick method for skipping the auth/login and go directly into app in offline mode * @param view */ public void clickedSkip(View view){ ArrayList<String> emails = new ArrayList<String>(); Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+ Account[] accounts = AccountManager.get(this.getApplicationContext()).getAccounts(); for (Account account : accounts) { if (emailPattern.matcher(account.name).matches()) { emails.add(account.name); } } ServerConnection.setConnectedToServer(false); User.setUserEmails(emails); //Creates an intent for switching to MainActivity Intent loginUser; loginUser = new Intent(this, MainActivity.class); startActivity(loginUser); } @Override public void onBackPressed() { new AlertDialog.Builder(this) .setTitle("Confirm Exit") .setMessage("Do you really want to exit?") .setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }) .setNegativeButton("No", null) .show(); } }