package sg.vinova.vss.group5.non.activity; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import oauth.signpost.OAuthConsumer; import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; import org.json.JSONObject; import sg.vinova.vss.group5.non.R; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.preference.PreferenceManager; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; public class LoginActivity extends Activity { private SharedPreferences prefs; //TextView console; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); this.prefs = PreferenceManager.getDefaultSharedPreferences(this); // Define Text Console //console = (TextView) findViewById(R.id.console); // Define button Button launchOauth = (Button) findViewById(R.id.launchOAuth); //Button clearCredentials = (Button) findViewById(R.id.delete_token); //Button getContacts = (Button) findViewById(R.id.get_contacts); //Button gotoMainscreen = (Button) findViewById(R.id.goto_mainscreen); // If click on GotoMainscreen => start new MainScreen /** gotoMainscreen.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { startActivity(new Intent().setClass(v.getContext(), MainScreen.class)); overridePendingTransition( R.anim.slide_in_left, R.anim.slide_out_left ); } }); */ // If click on launchOAuth => start new RequestTokenActivity launchOauth.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //startActivity(new Intent().setClass(v.getContext(), RequestTokenActivity.class)); startActivity(new Intent().setClass(v.getContext(), PrepareRequestTokenActivity.class)); overridePendingTransition( R.anim.slide_in_left, R.anim.slide_out_left ); } }); // If click on Delete Tokens => Do it /** clearCredentials.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { clearCredentials(); console.setText("Tokens deleted, getContacts call should fail now."); } }); */ //Feature.dial(this, "12345"); // If click on Get Contact => Do it /** getContacts.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { getContacts(); //Feature.dial(v.getContext(), "12345"); //Feature.sendSMS(v.getContext(), "12345"); //Feature.sendEmail(v.getContext(), "tungkudo94@gmail.com"); } }); */ } @Override public void onNewIntent(Intent intent) { Log.d(C.TAG, "pik 7a"); super.onNewIntent(intent); } @Override protected void onStart() { super.onStart(); } @Override protected void onPause() { super.onPause(); } @Override protected void onRestart() { // TODO Auto-generated method stub super.onRestart(); } @Override protected void onStop() { super.onStop(); } @Override protected void onDestroy() { super.onDestroy(); } /* public void launchOAuth(View view) { startActivity(new Intent().setClass(view.getContext(), RequestTokenActivity.class)); } */ private void getContacts() { String jsonOutput = ""; try { jsonOutput = makeSecuredReq(C.GET_CONTACTS_FROM_GOOGLE_REQUEST,getConsumer(this.prefs)); JSONObject jsonResponse = new JSONObject(jsonOutput); JSONObject m = (JSONObject)jsonResponse.get("feed"); JSONArray entries =(JSONArray)m.getJSONArray("entry"); String contacts = ""; for (int i=0 ; i<entries.length() ; i++) { JSONObject entry = entries.getJSONObject(i); JSONObject title = entry.getJSONObject("title"); if (title.getString("$t")!=null && title.getString("$t").length()>0) { contacts += title.getString("$t") + "\n"; } } // console.setText(contacts); } catch (Exception e) { Log.e(C.TAG, "Error executing request",e); // console.setText("Error retrieving contacts : " + jsonOutput); } } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); Log.d(C.TAG, "pik 7c"); Log.d(C.TAG, "pik 7bb"); if (isOAuthSuccessful()) { // OAuth successful, try getting the contacts //console.setText("OAuth successful, try getting the contacts"); startActivity(new Intent().setClass(this, MainScreen.class)); } else { //console.setText("OAuth failed, no tokens, Click on the Do OAuth Button."); } } /* public static void logout() { clearCredentials(); } */ private void clearCredentials() { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); final Editor edit = prefs.edit(); edit.remove("oauth_token"); edit.remove("oauth_token_secret"); edit.commit(); } private boolean isOAuthSuccessful() { try { Thread.sleep(3000); } catch(InterruptedException ex) { Thread.currentThread().interrupt(); } SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); String token = prefs.getString("oauth_token", null); String secret = prefs.getString("oauth_token_secret", null); Log.d(C.TAG, "pik6 " + token + " " + secret); if (token != null && secret != null) return true; else return false; } private OAuthConsumer getConsumer(SharedPreferences prefs) { String token = prefs.getString("oauth_token", ""); String secret = prefs.getString("oauth_token_secret", ""); OAuthConsumer consumer = new CommonsHttpOAuthConsumer(C.CONSUMER_KEY, C.CONSUMER_SECRET); consumer.setTokenWithSecret(token, secret); return consumer; } private String makeSecuredReq(String url,OAuthConsumer consumer) throws Exception { DefaultHttpClient httpclient = new DefaultHttpClient(); HttpGet request = new HttpGet(url); Log.i(C.TAG,"Requesting URL : " + url); consumer.sign(request); HttpResponse response = httpclient.execute(request); Log.i(C.TAG,"Statusline : " + response.getStatusLine()); InputStream data = response.getEntity().getContent(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(data)); String responeLine; StringBuilder responseBuilder = new StringBuilder(); while ((responeLine = bufferedReader.readLine()) != null) { responseBuilder.append(responeLine); } Log.i(C.TAG,"Response : " + responseBuilder.toString()); return responseBuilder.toString(); } }