Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
627 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
eclipse.preferences.version=1 | ||
encoding/<project>=UTF-8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.sakebook.android.uploadhelper" | ||
android:versionCode="1" | ||
android:versionName="1.0" > | ||
|
||
<uses-sdk | ||
android:minSdkVersion="11" | ||
android:targetSdkVersion="16" /> | ||
|
||
<application/> | ||
|
||
</manifest> |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<lint> | ||
</lint> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# To enable ProGuard in your project, edit project.properties | ||
# to define the proguard.config property as described in that file. | ||
# | ||
# Add project specific ProGuard rules here. | ||
# By default, the flags in this file are appended to flags specified | ||
# in ${sdk.dir}/tools/proguard/proguard-android.txt | ||
# You can edit the include path and order by changing the ProGuard | ||
# include property in project.properties. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# Add any project specific keep options here: | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# 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 edit | ||
# "ant.properties", and override values to adapt the script to your | ||
# project structure. | ||
# | ||
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): | ||
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt | ||
|
||
# Project target. | ||
target=android-17 | ||
android.library=true |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<resources> | ||
|
||
<!-- | ||
Base application theme for API 11+. This theme completely replaces | ||
AppBaseTheme from res/values/styles.xml on API 11+ devices. | ||
--> | ||
<style name="AppBaseTheme" parent="android:Theme.Holo.Light"> | ||
<!-- API 11 theme customizations can go here. --> | ||
</style> | ||
|
||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<resources> | ||
|
||
<!-- | ||
Base application theme for API 14+. This theme completely replaces | ||
AppBaseTheme from BOTH res/values/styles.xml and | ||
res/values-v11/styles.xml on API 14+ devices. | ||
--> | ||
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar"> | ||
<!-- API 14 theme customizations can go here. --> | ||
</style> | ||
|
||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<resources> | ||
|
||
<string name="app_name">UploadHelper</string> | ||
|
||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<resources> | ||
|
||
<!-- | ||
Base application theme, dependent on API level. This theme is replaced | ||
by AppBaseTheme from res/values-vXX/styles.xml on newer devices. | ||
--> | ||
<style name="AppBaseTheme" parent="android:Theme.Light"> | ||
<!-- | ||
Theme customizations available in newer API levels can go in | ||
res/values-vXX/styles.xml, while customizations related to | ||
backward-compatibility can go here. | ||
--> | ||
</style> | ||
|
||
<!-- Application theme. --> | ||
<style name="AppTheme" parent="AppBaseTheme"> | ||
<!-- All customizations that are NOT specific to a particular API-level can go here. --> | ||
</style> | ||
|
||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.sakebook.android.uploadhelper; | ||
|
||
public class Const { | ||
|
||
public static String TAG = "UploadHelper"; | ||
public static String CLIENT_ID = ""; | ||
public static String CLIENT_SECRET_ID = ""; | ||
|
||
} |
152 changes: 152 additions & 0 deletions
152
src/com/sakebook/android/uploadhelper/ImgurAuthorization.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package com.sakebook.android.uploadhelper; | ||
|
||
import android.content.Context; | ||
import android.content.SharedPreferences; | ||
import android.text.TextUtils; | ||
import android.util.Log; | ||
|
||
import org.apache.http.NameValuePair; | ||
import org.apache.http.client.entity.UrlEncodedFormEntity; | ||
import org.apache.http.message.BasicNameValuePair; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.util.ArrayList; | ||
import java.util.Scanner; | ||
|
||
public class ImgurAuthorization { | ||
|
||
private static ImgurAuthorization INSTANCE; | ||
|
||
static final String SHARED_PREFERENCES_NAME = "upload_helper_pref"; | ||
|
||
private ImgurAuthorization() {} | ||
|
||
public static ImgurAuthorization getInstance() { | ||
if (INSTANCE == null) | ||
INSTANCE = new ImgurAuthorization(); | ||
return INSTANCE; | ||
} | ||
|
||
public boolean isLoggedIn(Context context) { | ||
SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0); | ||
return !TextUtils.isEmpty(prefs.getString("access_token", null)); | ||
} | ||
|
||
public void addToHttpURLConnection(Context context, HttpURLConnection conn) { | ||
SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0); | ||
String accessToken = prefs.getString("access_token", null); | ||
|
||
if (!TextUtils.isEmpty(accessToken)) { | ||
conn.setRequestProperty("Authorization", "Bearer " + accessToken); | ||
} | ||
else { | ||
conn.setRequestProperty("Authorization", "Client-ID " + Const.CLIENT_ID); | ||
} | ||
} | ||
|
||
public void saveRefreshToken(Context context, String refreshToken, String accessToken, long expiresIn) { | ||
context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0) | ||
.edit() | ||
.putString("access_token", accessToken) | ||
.putString("refresh_token", refreshToken) | ||
.putLong("expires_in", expiresIn) | ||
.commit(); | ||
} | ||
|
||
public String requestNewAccessToken(Context context) { | ||
SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0); | ||
String refreshToken = prefs.getString("refresh_token", null); | ||
|
||
if (refreshToken == null) { | ||
Log.w(Const.TAG, "refresh token is null; cannot request access token. login first."); | ||
return null; | ||
} | ||
|
||
// clear previous access token | ||
prefs.edit().remove("access_token").commit(); | ||
|
||
HttpURLConnection conn = null; | ||
try { | ||
conn = (HttpURLConnection) new URL("https://api.imgur.com/oauth2/token").openConnection(); | ||
conn.setDoOutput(true); | ||
conn.setRequestProperty("Authorization", "Client-ID " + Const.CLIENT_ID); | ||
|
||
ArrayList<NameValuePair> nvps = new ArrayList<NameValuePair>(); | ||
nvps.add(new BasicNameValuePair("refresh_token", refreshToken)); | ||
nvps.add(new BasicNameValuePair("client_id", Const.CLIENT_ID)); | ||
nvps.add(new BasicNameValuePair("client_secret", Const.CLIENT_SECRET_ID)); | ||
nvps.add(new BasicNameValuePair("grant_type", "refresh_token")); | ||
|
||
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nvps); | ||
|
||
OutputStream out = conn.getOutputStream(); | ||
entity.writeTo(out); | ||
out.flush(); | ||
out.close(); | ||
|
||
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { | ||
InputStream in = conn.getInputStream(); | ||
handleAccessTokenResponse(context, in); | ||
in.close(); | ||
} | ||
else { | ||
Log.i(Const.TAG, "responseCode=" + conn.getResponseCode()); | ||
InputStream errorStream = conn.getErrorStream(); | ||
StringBuilder sb = new StringBuilder(); | ||
Scanner scanner = new Scanner(errorStream); | ||
while (scanner.hasNext()) { | ||
sb.append(scanner.next()); | ||
} | ||
Log.i(Const.TAG, "error response: " + sb.toString()); | ||
errorStream.close(); | ||
} | ||
|
||
return prefs.getString("access_token", null); | ||
|
||
} catch (Exception ex) { | ||
Log.e(Const.TAG, "Could not request new access token", ex); | ||
return null; | ||
} finally { | ||
try { | ||
conn.disconnect(); | ||
} catch (Exception ignore) {} | ||
} | ||
} | ||
|
||
private void handleAccessTokenResponse(Context context, InputStream in) throws JSONException { | ||
StringBuilder sb = new StringBuilder(); | ||
Scanner scanner = new Scanner(in); | ||
while (scanner.hasNext()) { | ||
sb.append(scanner.next()); | ||
} | ||
|
||
JSONObject root = new JSONObject(sb.toString()); | ||
String accessToken = root.getString("access_token"); | ||
String refreshToken = root.getString("refresh_token"); | ||
long expiresIn = root.getLong("expires_in"); | ||
String tokenType = root.getString("token_type"); | ||
String accountUsername = root.getString("account_username"); | ||
|
||
context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0) | ||
.edit() | ||
.putString("access_token", accessToken) | ||
.putString("refresh_token", refreshToken) | ||
.putLong("expires_in", expiresIn) | ||
.putString("token_type", tokenType) | ||
.putString("account_username", accountUsername) | ||
.commit(); | ||
} | ||
|
||
public void logout(Context context) { | ||
context.getSharedPreferences(SHARED_PREFERENCES_NAME, 0) | ||
.edit() | ||
.clear() | ||
.commit(); | ||
} | ||
|
||
} |
120 changes: 120 additions & 0 deletions
120
src/com/sakebook/android/uploadhelper/UploadHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package com.sakebook.android.uploadhelper; | ||
|
||
import android.content.Context; | ||
import android.net.Uri; | ||
import android.text.TextUtils; | ||
import android.util.Log; | ||
import android.view.View; | ||
|
||
public class UploadHelper implements UploadTaskCallback { | ||
|
||
private Context mContext; | ||
private UploadTaskCallback callback; | ||
private UploadTask task; | ||
private View v; | ||
|
||
/** | ||
* default constract | ||
* @param context Context; | ||
* @param callback Context; you need implement UploadTaskCallback; | ||
*/ | ||
public UploadHelper(Context context, UploadTaskCallback callback) { | ||
this.mContext = context; | ||
this.callback = callback; | ||
} | ||
|
||
/** | ||
* if you need enable target view. use this constract. | ||
* @param context Context; | ||
* @param callback Context; you need implement UploadTaskCallback; | ||
* @param v View; trigger upload view; | ||
*/ | ||
public UploadHelper(Context context, UploadTaskCallback callback, View v) { | ||
this.v = v; | ||
v.setEnabled(false); | ||
this.mContext = context; | ||
this.callback = callback; | ||
} | ||
|
||
/** | ||
* default upload method; | ||
* @param data Uri; ContentProvider; | ||
*/ | ||
public void uploadData(Uri data){ | ||
if (data != null) { | ||
task = new UploadTask(mContext, this); | ||
task.execute(data); | ||
} | ||
} | ||
|
||
/** | ||
* custom uplaod method; | ||
* @param data Uri; ContentProvider; | ||
* @param title String; dialog title text; | ||
* @param message String; dialog message text; | ||
*/ | ||
public void uploadData(Uri data, String title, String message){ | ||
if (data != null) { | ||
task = new UploadTask(mContext, this, title, message); | ||
task.execute(data); | ||
} | ||
} | ||
|
||
@Override | ||
public void success(String url) { | ||
if (!TextUtils.isEmpty(url)) { | ||
url = "http://imgur.com/"+url; | ||
Log.i(Const.TAG, "helper success: "+url); | ||
viewRecovery(); | ||
this.callback.success(url); | ||
}else { | ||
Log.i(Const.TAG, "helper success but null..."); | ||
viewRecovery(); | ||
this.callback.fail(url); | ||
} | ||
} | ||
|
||
@Override | ||
public void cancel(String message) { | ||
Log.i(Const.TAG, "helper cancel: "+message); | ||
viewRecovery(); | ||
this.callback.cancel(message); | ||
} | ||
|
||
@Override | ||
public void fail(String message) { | ||
Log.i(Const.TAG, "helper fail: "+message); | ||
viewRecovery(); | ||
this.callback.fail(message); | ||
} | ||
|
||
/** | ||
* Imgur client id; | ||
* @param clientId String; set Imgur client id; | ||
*/ | ||
public void setClientId(String clientId){ | ||
if (TextUtils.isEmpty(Const.CLIENT_ID)) { | ||
Const.CLIENT_ID = clientId; | ||
} | ||
} | ||
|
||
/** | ||
* Imgur secret id; | ||
* @param secretId String; set Imgur secret id; | ||
*/ | ||
public void setSecretId(String secretId){ | ||
if (TextUtils.isEmpty(Const.CLIENT_SECRET_ID)) { | ||
Const.CLIENT_SECRET_ID = secretId; | ||
} | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
private void viewRecovery(){ | ||
if (this.v != null) { | ||
v.setEnabled(false); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.