Permalink
Browse files

iinit

  • Loading branch information...
0 parents commit 0dbab785aab8bd343a55b94194d7d4d2194e96e7 @cho45 cho45 committed Feb 4, 2010
7 .classpath
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="src" path="gen"/>
+ <classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
3 .gitignore
@@ -0,0 +1,3 @@
+bin
+gen
+
33 .project
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>HatenaAccountSample</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>com.android.ide.eclipse.adt.ApkBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>com.android.ide.eclipse.adt.AndroidNature</nature>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
19 AndroidManifest.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.example.hatenaaccountsample"
+ android:versionCode="1"
+ android:versionName="1.0">
+ <application android:icon="@drawable/icon" android:label="@string/app_name">
+ <activity android:name=".HatenaAccountSample"
+ android:label="@string/app_name">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ <category android:name="android.intent.category.LAUNCHER" />
+ </intent-filter>
+ </activity>
+
+ <activity android:name=".Setting" android:screenOrientation="behind" android:configChanges="orientation|keyboardHidden"></activity>
+ </application>
+ <uses-sdk android:minSdkVersion="4" />
+ <uses-permission android:name="android.permission.INTERNET"></uses-permission>
+</manifest>
13 default.properties
@@ -0,0 +1,13 @@
+# This file is automatically generated by Android Tools.
+# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
+#
+# This file must be checked in Version Control Systems.
+#
+# To customize properties used by the Ant build system use,
+# "build.properties", and override values to adapt the script to your
+# project structure.
+
+# Indicates whether an apk should be generated for each density.
+split.density=false
+# Project target.
+target=android-4
BIN res/drawable-hdpi/icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN res/drawable-ldpi/icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN res/drawable-mdpi/icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 res/layout/main.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:orientation="vertical"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+ >
+
+ <TextView
+ android:id="@+id/info"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ />
+
+ <Button
+ android:text="Open Setting"
+ android:id="@+id/open_setting"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"/>
+</LinearLayout>
21 res/values/strings.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+ <string name="hello">Hello World, HatenaAccountSample!</string>
+ <string name="app_name">HatenaAccountSample</string>
+
+ <string name="cancel">Cancel</string>
+ <string name="yes">Yes</string>
+ <string name="no">No</string>
+ <string name="ok">OK</string>
+
+ <string name="account_title">Login</string>
+ <string name="account_title_logout">Logout</string>
+ <string name="account_summary">You're currently not logged in</string>
+ <string name="account_summary_loggedin">Logged in as %1$s</string>
+
+ <string name="already_logged_in">Logout</string>
+ <string name="already_logged_in_message">You're already logged in as %1$s. Logout from this account?</string>
+
+ <string name="install_hatena_account_title">Install Hatena Account Manager?</string>
+ <string name="install_hatena_account_message">This application requires Hatena Account Manager. Tap "OK" to access Android Market.</string>
+</resources>
7 res/xml/preferences.xml
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="utf-8"?>
+<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
+ <PreferenceScreen
+ android:key="account"
+ android:title="@string/account_title"
+ android:summary="@string/account_summary"/>
+</PreferenceScreen>
67 src/com/example/hatenaaccountsample/HatenaAccountSample.java
@@ -0,0 +1,67 @@
+package com.example.hatenaaccountsample;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.HttpResponse;
+
+import android.content.Intent;
+import android.os.Bundle;
+import android.view.View;
+import android.widget.Button;
+import android.widget.TextView;
+
+public class HatenaAccountSample extends HatenaActivity {
+ protected TextView mInfoView;
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.main);
+
+ mInfoView = (TextView)findViewById(R.id.info);
+
+ ((Button)findViewById(R.id.open_setting)).setOnClickListener(new Button.OnClickListener() {
+ public void onClick(View v) {
+ startActivity(new Intent(HatenaAccountSample.this, Setting.class));
+ }
+ });
+ }
+
+ @Override
+ public void onResume() {
+ super.onResume();
+ updateInfo();
+ }
+
+ protected void updateInfo() {
+ String username = mPref.getString(Setting.ACCOUNT_NAME, null);
+ mInfoView.setText(String.format("Username: %s", username));
+
+ mExecutor.execute(new Runnable() { public void run () {
+ try {
+ HttpResponse res = request(new HttpGet(String.format("http://b.hatena.ne.jp/my.name?%d", System.currentTimeMillis())));
+
+ final StringBuilder body = new StringBuilder();
+ try {
+ BufferedReader br = new BufferedReader(new InputStreamReader(res.getEntity().getContent()));
+ String line;
+ while ((line = br.readLine()) != null) {
+ body.append(line).append("\n");
+ }
+ br.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ runOnUiThread(new Runnable() { public void run () {
+ mInfoView.setText(String.format("b.hatena.ne.jp/my.name: %s", body.toString()));
+ } });
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ } });
+ }
+
+}
87 src/com/example/hatenaaccountsample/HatenaActivity.java
@@ -0,0 +1,87 @@
+package com.example.hatenaaccountsample;
+
+import java.io.IOException;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ExecutorService;
+
+import org.apache.http.client.CookieStore;
+import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.client.params.HttpClientParams;
+import org.apache.http.conn.scheme.PlainSocketFactory;
+import org.apache.http.conn.scheme.Scheme;
+import org.apache.http.conn.scheme.SchemeRegistry;
+import org.apache.http.HttpHost;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpVersion;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
+import org.apache.http.impl.cookie.BasicClientCookie;
+import org.apache.http.params.BasicHttpParams;
+import org.apache.http.params.HttpConnectionParams;
+import org.apache.http.params.HttpParams;
+import org.apache.http.params.HttpProtocolParams;
+import org.apache.http.protocol.HTTP;
+
+import android.app.Activity;
+import android.content.SharedPreferences;
+import android.os.Bundle;
+import android.preference.PreferenceManager;
+import android.util.Log;
+
+abstract class HatenaActivity extends Activity {
+ private static final String TAG = "HatenaActivity";
+
+ private static DefaultHttpClient sHttpClient;
+ static {
+ SchemeRegistry schemeRegistry = new SchemeRegistry();
+ schemeRegistry.register(
+ new Scheme(
+ HttpHost.DEFAULT_SCHEME_NAME,
+ PlainSocketFactory.getSocketFactory(),
+ 80
+ )
+ );
+
+ HttpParams httpParams = new BasicHttpParams();
+ HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
+ HttpProtocolParams.setContentCharset(httpParams, HTTP.UTF_8);
+
+ sHttpClient = new DefaultHttpClient(
+ new ThreadSafeClientConnManager(httpParams, schemeRegistry),
+ httpParams
+ );
+
+ HttpConnectionParams.setSoTimeout(sHttpClient.getParams(), 1000 * 30);
+ HttpConnectionParams.setConnectionTimeout(sHttpClient.getParams(), 1000 * 5);
+ HttpClientParams.setRedirecting(sHttpClient.getParams(), false);
+ }
+
+ protected SharedPreferences mPref;
+ protected ExecutorService mExecutor;
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ mPref = PreferenceManager.getDefaultSharedPreferences(this);
+ mExecutor = Executors.newCachedThreadPool();
+ }
+
+ @Override
+ public void onDestroy() {
+ super.onDestroy();
+ mExecutor.shutdownNow();
+ }
+
+ public HttpResponse request(HttpUriRequest req) throws IOException {
+ String rk = mPref.getString(Setting.ACCOUNT_RK, null);
+
+ if (rk != null) {
+ BasicClientCookie cookie = new BasicClientCookie("rk", rk);
+ cookie.setDomain(".hatena.ne.jp");
+ sHttpClient.getCookieStore().addCookie(cookie);
+ }
+
+ return sHttpClient.execute(req);
+ }
+}
137 src/com/example/hatenaaccountsample/Setting.java
@@ -0,0 +1,137 @@
+package com.example.hatenaaccountsample;
+
+import android.app.AlertDialog;
+import android.content.ActivityNotFoundException;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.net.Uri;
+import android.os.Bundle;
+import android.preference.Preference;
+import android.preference.PreferenceActivity;
+import android.preference.PreferenceManager;
+import android.preference.PreferenceScreen;
+
+public class Setting extends PreferenceActivity {
+ public static final String MODE = "mode";
+
+ public static final String ACCOUNT = "account";
+ public static final String ACCOUNT_NAME = "account_name";
+ public static final String ACCOUNT_PASSWORD = "account_password";
+ public static final String ACCOUNT_RK = "account_rk";
+ public static final String ACCOUNT_RKM = "account_rkm";
+
+ protected static final int USER_REQUEST = 1;
+ protected SharedPreferences mPref;
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ addPreferencesFromResource(R.xml.preferences);
+
+ mPref = PreferenceManager.getDefaultSharedPreferences(this);
+
+ Intent intent = getIntent();
+ String mode = intent.getStringExtra(MODE);
+
+ // 他の Activity から
+ // startActivity(new Intent(this, Setting.class).putExtra(Setting.MODE, Setting.ACCOUNT));
+ // で起動した場合、再認証する
+ if (mode != null && mode.equals(ACCOUNT)) {
+ final String username = mPref.getString(ACCOUNT_NAME, null);
+ userRequest(username);
+ }
+
+ updateAccountSummary();
+
+ PreferenceScreen pAccount = (PreferenceScreen) findPreference(ACCOUNT);
+ pAccount.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
+ public boolean onPreferenceClick(Preference preference) {
+ String username = getPreferenceManager().getSharedPreferences().getString(ACCOUNT_NAME, null);
+ if (username != null) {
+ // 既にログインしている場合はログアウトするダイアログ
+ new AlertDialog.Builder(Setting.this)
+ .setIcon(R.drawable.icon)
+ .setTitle(R.string.already_logged_in)
+ .setMessage(getString(R.string.already_logged_in_message, username))
+ .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
+ public void onClick(DialogInterface dialogInterface, int i) {
+ SharedPreferences.Editor editor = mPref.edit();
+ editor.remove(ACCOUNT_NAME);
+ editor.remove(ACCOUNT_RK);
+ editor.remove(ACCOUNT_RKM);
+ editor.commit();
+ updateAccountSummary();
+ }
+ })
+ .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
+ public void onClick(DialogInterface dialogInterface, int i) {
+ }
+ })
+ .show();
+ } else {
+ // ログインしてないならログインリクエスト
+ userRequest(null);
+ }
+ return true;
+ }
+ });
+ }
+
+ private void updateAccountSummary () {
+ // 現在の状態をユーザに表示
+ PreferenceScreen pAccount = (PreferenceScreen) findPreference(ACCOUNT);
+ String username = mPref.getString(ACCOUNT_NAME, null);
+ if (username != null) {
+ pAccount.setTitle(getString(R.string.account_title_logout));
+ pAccount.setSummary(getString(R.string.account_summary_loggedin, username));
+ } else {
+ pAccount.setTitle(getString(R.string.account_title));
+ pAccount.setSummary(getString(R.string.account_summary));
+ }
+ }
+
+ protected void userRequest(String username) {
+ // 必要に応じてアカウント管理へユーザ情報をリクエスト
+ // アカウント管理がインストールされてない場合はマーケットへ
+ Intent intent = new Intent(Intent.ACTION_PICK);
+ intent.setType("vnd.android.cursor.dir/vnd.hatena.accounts");
+ if (username != null) intent.putExtra("account", username);
+ try {
+ startActivityForResult(intent, USER_REQUEST);
+ } catch (ActivityNotFoundException e) {
+ new AlertDialog.Builder(this)
+ .setTitle(R.string.install_hatena_account_title)
+ .setMessage(R.string.install_hatena_account_message)
+ .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
+ public void onClick(DialogInterface dialogInterface, int i) {
+ Uri uri = Uri.parse("market://search?q=pname:com.hatena.android.accounts");
+ Intent intent = new Intent(Intent.ACTION_VIEW, uri);
+ startActivity(intent);
+ }
+ })
+ .show();
+ }
+ }
+
+ @Override
+ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+ if (requestCode == USER_REQUEST) {
+ if (resultCode == RESULT_OK) {
+ // 取得した情報を保存しておく
+ String username = data.getStringExtra("username");
+ String randomkey = data.getStringExtra("randomkey");
+ String rkm = data.getStringExtra("rkm");
+
+ SharedPreferences.Editor editor = mPref.edit();
+ editor.putString(ACCOUNT_NAME, username);
+ editor.putString(ACCOUNT_RK, randomkey);
+ editor.putString(ACCOUNT_RKM, rkm);
+ editor.commit();
+
+ updateAccountSummary();
+ }
+ }
+ }
+}
+

0 comments on commit 0dbab78

Please sign in to comment.