<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
+ <activity android:name="Authorize"
+ android:label="@string/app_name">
+ </activity>
<activity android:name="WhereAmIprefs"
android:label="@string/app_name">
</activity>
<item android:id="@+id/quit"
android:icon="@android:drawable/ic_menu_close_clear_cancel"
android:title="@string/quit" />
+ <item android:id="@+id/authorize"
+ android:icon="@android:drawable/ic_lock_lock"
+ android:title="@string/authorize" />
<item android:id="@+id/settings"
android:icon="@android:drawable/ic_menu_preferences"
android:title="@string/settings" />
<string name="lasttry">Последнее обновление</string>
<string name="failtry">Неудачная попытка</string>
<string name="quit">Выход</string>
+ <string name="authorize">Авторизация</string>
<string name="settings">Конфигурация</string>
<string name="fullscreentitle">Полный экран</string>
<string name="fullscreensummary">Полный экран или с индикаторами</string>
--- /dev/null
+// http://blog.doityourselfandroid.com/2011/08/06/oauth-2-0-flow-android/
+// http://code.google.com/p/google-api-java-client/wiki/OAuth2Draft10
+
+package org.average.whereami;
+
+import java.io.IOException;
+
+import org.average.whereami.ClientCredentials;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.graphics.Bitmap;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.content.SharedPreferences.Editor;
+import android.preference.PreferenceManager;
+import android.util.Log;
+import android.view.View;
+import android.webkit.WebView;
+import android.webkit.WebViewClient;
+
+import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse;
+import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessTokenRequest.GoogleAuthorizationCodeGrant;
+import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAuthorizationRequestUrl;
+import com.google.api.client.http.javanet.NetHttpTransport;
+import com.google.api.client.json.jackson.JacksonFactory;
+
+public class Authorize extends Activity {
+
+ final String TAG = getClass().getName();
+
+ private SharedPreferences prefs;
+ private static final String ACCESS_TOKEN = "access_token";
+ private static final String EXPIRES_IN = "expires_in";
+ private static final String REFRESH_TOKEN = "refresh_token";
+ private static final String SCOPE = "scope";
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ Log.w(TAG, "Starting task to retrieve request token.");
+ prefs = PreferenceManager.getDefaultSharedPreferences(this);
+ }
+
+ @Override
+ protected void onResume() {
+ super.onResume();
+ WebView webview = new WebView(this);
+ webview.getSettings().setJavaScriptEnabled(true);
+ webview.setVisibility(View.VISIBLE);
+ setContentView(webview);
+ String authorizationUrl = new GoogleAuthorizationRequestUrl(
+ ClientCredentials.CLIENT_ID,
+ ClientCredentials.REDIRECT_URI,
+ ClientCredentials.SCOPE).build();
+ webview.setWebViewClient(new WebViewClient() {
+ @Override
+ public void onPageStarted(WebView view, String url, Bitmap bitmap) {
+ Log.w(TAG, "onPageStarted : " + url);
+ }
+ @Override
+ public void onPageFinished(WebView view, String url) {
+ if (url.startsWith(ClientCredentials.REDIRECT_URI)) {
+ try {
+ if (url.indexOf("code=")!=-1) {
+ String code = extractCodeFromUrl(url);
+ AccessTokenResponse accessTokenResponse =
+ new GoogleAuthorizationCodeGrant(
+ new NetHttpTransport(),
+ new JacksonFactory(),
+ ClientCredentials.CLIENT_ID,
+ ClientCredentials.CLIENT_SECRET,
+ code,
+ ClientCredentials.REDIRECT_URI).execute();
+ storeTokens(accessTokenResponse);
+ finish();
+ //view.setVisibility(View.INVISIBLE);
+ //startActivity(new Intent(Authorize.this,
+ // WhereAmI.class));
+ } else if (url.indexOf("error=")!=-1) {
+ clearTokens();
+ finish();
+ //view.setVisibility(View.INVISIBLE);
+ //startActivity(new Intent(Authorize.this,
+ // WhereAmI.class));
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ Log.w(TAG, "onPageFinished : " + url);
+ }
+
+ private String extractCodeFromUrl(String url) {
+ return url.substring(
+ ClientCredentials.REDIRECT_URI.length()+7,
+ url.length());
+ }
+
+ private void storeTokens(AccessTokenResponse accessTokenResponse) {
+ Log.w(TAG, "Storing tokens: " + accessTokenResponse);
+ Editor editor = prefs.edit();
+ editor.putString(ACCESS_TOKEN,
+ accessTokenResponse.accessToken);
+ editor.putLong( EXPIRES_IN,
+ accessTokenResponse.expiresIn);
+ editor.putString(REFRESH_TOKEN,
+ accessTokenResponse.refreshToken);
+ editor.putString(SCOPE,
+ accessTokenResponse.scope);
+ editor.commit();
+ }
+
+ private void clearTokens() {
+ Log.w(TAG, "Clear tokens");
+ Editor editor = prefs.edit();
+ editor.remove(ACCESS_TOKEN);
+ editor.remove(EXPIRES_IN);
+ editor.remove(REFRESH_TOKEN);
+ editor.remove(SCOPE);
+ editor.commit();
+ }
+ });
+
+ webview.loadUrl(authorizationUrl);
+ }
+}
import android.os.SystemClock;
public final class Location extends Oracle {
+
+ final String TAG = getClass().getName();
+
@Override
public final String getResult() {
SystemClock.sleep(6000);
import android.os.SystemClock;
public final class PhoneLog extends Oracle {
+
+ final String TAG = getClass().getName();
+
@Override
public final String getResult() {
SystemClock.sleep(5000);
public class WhereAmI extends Activity
{
+ final String TAG = getClass().getName();
+
private WifiManager wifiman;
private Boolean managewifi = false;
private Long updatedelay = 60000L;
private class BgUpdate extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
- Log.w("WhereAmI", "BgUpdate " + updater + " starting");
+ Log.w(TAG, "BgUpdate " + updater + " starting");
return updater.getResult();
}
@Override
protected void onPostExecute(String str) {
- Log.w("WhereAmI", "BgUpdate callback executing");
+ Log.w(TAG, "BgUpdate callback executing");
tv.setText(str);
runningtasks--;
if (runningtasks <= 0) {
boolean wifion = wifiman.setWifiEnabled(false);
- Log.w("WhereAmI", "disabling wifi result " + wifion);
+ Log.w(TAG, "disabling wifi result " + wifion);
Time tm = new Time();
tm.setToNow();
tvs.setText(R.string.lasttry);
@Override
protected void onCancelled() {
- Log.w("WhereAmI", "BgUpdate callback cancelled");
+ Log.w(TAG, "BgUpdate callback cancelled");
runningtasks--;
}
}
private Runnable updateInfo = new Runnable () {
public void run() {
- Log.w("WhereAmI", "updateInfo starting");
+ Log.w(TAG, "updateInfo starting");
if (managewifi) {
IntentFilter intentFilter =
new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
registerReceiver(connChanged,intentFilter);
connChangedRegistered = true;
boolean wifion = wifiman.setWifiEnabled(true);
- Log.w("WhereAmI", "enabling wifi result " + wifion);
+ Log.w(TAG, "enabling wifi result " + wifion);
} else {
for (int i = 0; i < ut.length; i++) {
runningtasks++;
private Runnable resetInfo = new Runnable () {
public void run() {
- Log.w("WhereAmI", "resetInfo starting");
+ Log.w(TAG, "resetInfo starting");
if (connChangedRegistered) {
unregisterReceiver(connChanged);
connChangedRegistered = false;
}
if (managewifi) {
boolean wifion = wifiman.setWifiEnabled(false);
- Log.w("WhereAmI", "disabling wifi result " + wifion);
+ Log.w(TAG, "disabling wifi result " + wifion);
}
}
};
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
- Log.w("WhereAmI", "Connectivity changed to " + isConnected);
+ Log.w(TAG, "Connectivity changed to " + isConnected);
if (isConnected) {
for (int i = 0; i < ut.length; i++) {
runningtasks++;
new UpdateTarget((TextView)findViewById(R.id.phonecall),
new Location())
};
- Log.w("WhereAmI", "created UI, about to start update task");
+ Log.w(TAG, "created UI, about to start update task");
mHandler.post(updateClock);
mHandler.post(updateCal);
mHandler.post(updateInfo);
- Log.w("WhereAmI", "created UI, update task created");
+ Log.w(TAG, "created UI, update task created");
}
/** Called when reactivated */
@Override
public void onPause() {
super.onPause();
- Log.w("WhereAmI", "going background");
+ Log.w(TAG, "going background");
}
/** Called when the activity is destroyed. */
@Override
public void onDestroy() {
super.onDestroy();
- Log.w("WhereAmI", "going down");
+ Log.w(TAG, "going down");
mHandler.removeCallbacks(updateClock);
mHandler.removeCallbacks(updateCal);
mHandler.removeCallbacks(updateInfo);
connChangedRegistered = false;
}
boolean wifion = wifiman.setWifiEnabled(false);
- Log.w("WhereAmI", "disabling wifi result " + wifion);
+ Log.w(TAG, "disabling wifi result " + wifion);
}
/** Called when the menu is activated. */
case R.id.quit:
finish();
return true;
+ case R.id.authorize:
+ Log.w(TAG, "authorize requested");
+ startActivity(new Intent(this, Authorize.class));
+ return true;
case R.id.settings:
- Log.w("WhereAmI", "settings requested");
+ Log.w(TAG, "settings requested");
startActivity(new Intent(this, WhereAmIprefs.class));
return true;
default: