package com.omokageru.ak.twitter; import java.io.IOException; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.auth.AuthScope; import org.apache.http.auth.Credentials; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class SetID extends Activity { private String orgid; private String orgpass; private String tempid; private String temppass; private EditText idEdit; private EditText passEdit; private Button saveButton; private Button cancelButton; /* (非 Javadoc) * @see android.app.Activity#onCreate(android.os.Bundle) */ @Override protected void onCreate(Bundle savedInstanceState) { // TODO 自動生成されたメソッド・スタブ super.onCreate(savedInstanceState); setContentView(R.layout.setid); idEdit = (EditText) findViewById(R.id.setid_edit); passEdit = (EditText) findViewById(R.id.setpass_edit); saveButton = (Button) findViewById(R.id.button_save); cancelButton = (Button) findViewById(R.id.button_cancel); // プリファレンスからID/Passを取得 SharedPreferences preferences = getSharedPreferences( getString(R.string.preferences_name), MODE_PRIVATE); orgid = preferences.getString( getString(R.string.preferences_id), ""); orgpass = preferences.getString( getString(R.string.preferences_pass), ""); // 初期値の設定 idEdit.setText(orgid); passEdit.setText(orgpass); tempid = ""; temppass = ""; saveButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO 自動生成されたメソッド・スタブ tempid = idEdit.getEditableText().toString(); temppass = passEdit.getEditableText().toString(); // tempid == null とか tempid == "" だと引っかからなかったので if( tempid.length() == 0 || temppass.length() == 0){ Toast.makeText( SetID.this, "ID / pass の設定がありません", Toast.LENGTH_LONG).show(); return; } // id/pass が正しいかを確認 if( !chkid()){ Toast.makeText( SetID.this, "ID / pass を見直してください", Toast.LENGTH_LONG).show(); return; } // 新しい ID/pass を設定して終了 SharedPreferences preferences = getSharedPreferences( getString(R.string.preferences_name), MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); editor.putString( getString(R.string.preferences_id), tempid); editor.putString( getString(R.string.preferences_pass), temppass); editor.commit(); setResult(RESULT_OK); finish(); } }); cancelButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO 自動生成されたメソッド・スタブ // cancel といえども、まったく設定のない場合は許可しない if( orgid.length() == 0 || orgpass.length() == 0){ Toast.makeText( SetID.this, "ID / pass の設定がありません", Toast.LENGTH_LONG).show(); return; } setResult(RESULT_CANCELED); finish(); } }); } private boolean chkid(){ try { DefaultHttpClient httpClient = new DefaultHttpClient(); Credentials cred = new UsernamePasswordCredentials( tempid, temppass); httpClient.getCredentialsProvider().setCredentials( new AuthScope("twitter.com", 80), cred); HttpGet get = new HttpGet( "http://twitter.com/account/verify_credentials.json"); HttpResponse response = httpClient.execute(get); // 失敗時の処理 if(response.getStatusLine().getStatusCode() != HttpStatus.SC_OK){ return false; } // 一応セッションを閉じておく HttpPost post = new HttpPost( "http://twitter.com/account/end_session.json"); httpClient.execute(post); return true; } catch (ClientProtocolException e) { // TODO 自動生成された catch ブロック Toast.makeText( SetID.this, "予期せぬエラー SetID#chkid ClientProtocolException", Toast.LENGTH_LONG).show(); } catch (IOException e) { // TODO 自動生成された catch ブロック Toast.makeText( SetID.this, "予期せぬエラー SetID#chkid IOException", Toast.LENGTH_LONG).show(); } return false; } }