Android: switch from AQuery to Picasso (+OkHttp)

This commit is contained in:
jendib 2016-01-16 22:06:48 +01:00
parent b9a4f0f1e0
commit e7a289ffb5
15 changed files with 206 additions and 137 deletions

View File

@ -12,10 +12,7 @@
<option name="SELECTED_TEST_ARTIFACT" value="_android_test_" /> <option name="SELECTED_TEST_ARTIFACT" value="_android_test_" />
<option name="ASSEMBLE_TASK_NAME" value="assembleDebug" /> <option name="ASSEMBLE_TASK_NAME" value="assembleDebug" />
<option name="COMPILE_JAVA_TASK_NAME" value="compileDebugSources" /> <option name="COMPILE_JAVA_TASK_NAME" value="compileDebugSources" />
<option name="ASSEMBLE_TEST_TASK_NAME" value="assembleDebugAndroidTest" />
<option name="COMPILE_JAVA_TEST_TASK_NAME" value="compileDebugAndroidTestSources" />
<afterSyncTasks> <afterSyncTasks>
<task>generateDebugAndroidTestSources</task>
<task>generateDebugSources</task> <task>generateDebugSources</task>
</afterSyncTasks> </afterSyncTasks>
<option name="ALLOW_USER_CONFIGURATION" value="false" /> <option name="ALLOW_USER_CONFIGURATION" value="false" />
@ -28,7 +25,7 @@
</component> </component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="false"> <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/build/intermediates/classes/debug" /> <output url="file://$MODULE_DIR$/build/intermediates/classes/debug" />
<output-test url="file://$MODULE_DIR$/build/intermediates/classes/androidTest/debug" /> <output-test url="file://$MODULE_DIR$/build/intermediates/classes/test/debug" />
<exclude-output /> <exclude-output />
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/r/debug" isTestSource="false" generated="true" /> <sourceFolder url="file://$MODULE_DIR$/build/generated/source/r/debug" isTestSource="false" generated="true" />
@ -50,6 +47,13 @@
<sourceFolder url="file://$MODULE_DIR$/src/debug/java" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src/debug/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/jni" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src/debug/jni" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/rs" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src/debug/rs" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/res" type="java-resource" /> <sourceFolder url="file://$MODULE_DIR$/src/main/res" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" /> <sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/main/assets" type="java-resource" /> <sourceFolder url="file://$MODULE_DIR$/src/main/assets" type="java-resource" />
@ -57,6 +61,13 @@
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" /> <sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" /> <sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" /> <sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
@ -66,6 +77,7 @@
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" /> <sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/builds" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/appcompat-v7/23.1.1/jars" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/appcompat-v7/23.1.1/jars" />
@ -76,12 +88,11 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/it.sephiroth.android.library.imagezoom/imagezoom/1.0.5/jars" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/it.sephiroth.android.library.imagezoom/imagezoom/1.0.5/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-classes" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-runtime-classes" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-runtimfe-classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-verifier" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-verifier" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant-run-support" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant-run-support" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/pre-dexed" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/reload-dex" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/reload-dex" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/restart-dex" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/restart-dex" />
@ -93,18 +104,20 @@
</content> </content>
<orderEntry type="jdk" jdkName="Android API 23 Platform" jdkType="Android SDK" /> <orderEntry type="jdk" jdkName="Android API 23 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="recyclerview-v7-23.1.1" level="project" />
<orderEntry type="library" exported="" name="support-annotations-23.1.1" level="project" /> <orderEntry type="library" exported="" name="support-annotations-23.1.1" level="project" />
<orderEntry type="library" exported="" name="android-easing-1.0.3" level="project" />
<orderEntry type="library" exported="" name="imagezoom-1.0.5" level="project" />
<orderEntry type="library" exported="" name="android-async-http-1.4.9" level="project" />
<orderEntry type="library" exported="" name="picasso-2.5.2" level="project" />
<orderEntry type="library" exported="" name="eventbus-2.4.1" level="project" />
<orderEntry type="library" exported="" name="recyclerview-v7-23.1.1" level="project" />
<orderEntry type="library" exported="" name="support-v4-23.1.1" level="project" /> <orderEntry type="library" exported="" name="support-v4-23.1.1" level="project" />
<orderEntry type="library" exported="" name="fab-0.0.6" level="project" /> <orderEntry type="library" exported="" name="fab-0.0.6" level="project" />
<orderEntry type="library" exported="" name="android-easing-1.0.3" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-23.1.1" level="project" /> <orderEntry type="library" exported="" name="appcompat-v7-23.1.1" level="project" />
<orderEntry type="library" exported="" name="imagezoom-1.0.5" level="project" />
<orderEntry type="library" exported="" name="httpclient-4.3.6" level="project" /> <orderEntry type="library" exported="" name="httpclient-4.3.6" level="project" />
<orderEntry type="library" exported="" name="android-query.0.26.8" level="project" /> <orderEntry type="library" exported="" name="okio-1.6.0" level="project" />
<orderEntry type="library" exported="" name="picasso2-okhttp3-downloader-1.0.2" level="project" />
<orderEntry type="library" exported="" name="tokenautocomplete-1.2.1" level="project" /> <orderEntry type="library" exported="" name="tokenautocomplete-1.2.1" level="project" />
<orderEntry type="library" exported="" name="android-async-http-1.4.9" level="project" /> <orderEntry type="library" exported="" name="okhttp-3.0.1" level="project" />
<orderEntry type="library" exported="" name="eventbus-2.4.1" level="project" />
<orderEntry type="library" exported="" name="org.apache.http.legacy-android-23" level="project" />
</component> </component>
</module> </module>

View File

@ -3,7 +3,7 @@ buildscript {
jcenter() jcenter()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:2.0.0-alpha3' classpath 'com.android.tools.build:gradle:2.0.0-alpha5'
} }
} }
apply plugin: 'com.android.application' apply plugin: 'com.android.application'
@ -15,7 +15,6 @@ repositories {
android { android {
compileSdkVersion 23 compileSdkVersion 23
buildToolsVersion '23.0.2' buildToolsVersion '23.0.2'
useLibrary 'org.apache.http.legacy'
defaultConfig { defaultConfig {
minSdkVersion 14 minSdkVersion 14
@ -57,4 +56,7 @@ dependencies {
compile 'it.sephiroth.android.library.imagezoom:imagezoom:1.0.5' compile 'it.sephiroth.android.library.imagezoom:imagezoom:1.0.5'
compile 'de.greenrobot:eventbus:2.4.1' compile 'de.greenrobot:eventbus:2.4.1'
compile 'com.shamanland:fab:0.0.6' compile 'com.shamanland:fab:0.0.6'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.squareup.okhttp3:okhttp:3.0.1'
compile 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.0.2'
} }

View File

@ -2,7 +2,6 @@ package com.sismics.docs;
import android.app.Application; import android.app.Application;
import com.androidquery.callback.BitmapAjaxCallback;
import com.sismics.docs.model.application.ApplicationContext; import com.sismics.docs.model.application.ApplicationContext;
import com.sismics.docs.util.PreferenceUtil; import com.sismics.docs.util.PreferenceUtil;
@ -24,10 +23,4 @@ public class MainApplication extends Application {
super.onCreate(); super.onCreate();
} }
@Override
public void onLowMemory() {
super.onLowMemory();
BitmapAjaxCallback.clearCache();
}
} }

View File

@ -731,7 +731,7 @@ public class DocumentViewActivity extends AppCompatActivity {
@Override @Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) { public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
JSONArray comments = response.optJSONArray("comments"); JSONArray comments = response.optJSONArray("comments");
commentListAdapter = new CommentListAdapter(comments); commentListAdapter = new CommentListAdapter(DocumentViewActivity.this, comments);
listView.setAdapter(commentListAdapter); listView.setAdapter(commentListAdapter);
listView.setVisibility(View.VISIBLE); listView.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE); progressBar.setVisibility(View.GONE);

View File

@ -10,8 +10,8 @@ import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.TextView;
import com.androidquery.AQuery;
import com.sismics.docs.R; import com.sismics.docs.R;
import com.sismics.docs.listener.CallbackListener; import com.sismics.docs.listener.CallbackListener;
import com.sismics.docs.listener.JsonHttpResponseHandler; import com.sismics.docs.listener.JsonHttpResponseHandler;
@ -43,19 +43,17 @@ public class LoginActivity extends AppCompatActivity {
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.login_activity); setContentView(R.layout.login_activity);
AQuery aq = new AQuery(this); TextView loginExplainTextView = (TextView) findViewById(R.id.loginExplain);
aq.id(R.id.loginExplain) loginExplainTextView.setText(Html.fromHtml(getString(R.string.login_explain)));
.text(Html.fromHtml(getString(R.string.login_explain))) loginExplainTextView.setMovementMethod(LinkMovementMethod.getInstance());
.getTextView()
.setMovementMethod(LinkMovementMethod.getInstance()); final EditText txtServer = (EditText) findViewById(R.id.txtServer);
final EditText txtUsername = (EditText) findViewById(R.id.txtUsername);
final EditText txtServer = aq.id(R.id.txtServer).getEditText(); final EditText txtPassword = (EditText) findViewById(R.id.txtPassword);
final EditText txtUsername = aq.id(R.id.txtUsername).getEditText(); final Button btnConnect = (Button) findViewById(R.id.btnConnect);
final EditText txtPassword = aq.id(R.id.txtPassword).getEditText(); loginForm = findViewById(R.id.loginForm);
final Button btnConnect = aq.id(R.id.btnConnect).getButton(); progressBar = findViewById(R.id.progressBar);
loginForm = aq.id(R.id.loginForm).getView();
progressBar = aq.id(R.id.progressBar).getView();
PreferenceManager.setDefaultValues(this, R.xml.preferences, false); PreferenceManager.setDefaultValues(this, R.xml.preferences, false);

View File

@ -18,7 +18,6 @@ import android.widget.ListView;
import android.widget.SearchView; import android.widget.SearchView;
import android.widget.TextView; import android.widget.TextView;
import com.androidquery.util.AQUtility;
import com.sismics.docs.R; import com.sismics.docs.R;
import com.sismics.docs.adapter.TagListAdapter; import com.sismics.docs.adapter.TagListAdapter;
import com.sismics.docs.event.AdvancedSearchEvent; import com.sismics.docs.event.AdvancedSearchEvent;
@ -274,10 +273,6 @@ public class MainActivity extends AppCompatActivity {
@Override @Override
protected void onDestroy() { protected void onDestroy() {
EventBus.getDefault().unregister(this); EventBus.getDefault().unregister(this);
if(isTaskRoot()) {
int cacheSizeMb = PreferenceUtil.getIntegerPreference(this, PreferenceUtil.PREF_CACHE_SIZE, 10);
AQUtility.cleanCacheAsync(this, cacheSizeMb * 1000000, cacheSizeMb * 1000000);
}
super.onDestroy(); super.onDestroy();
} }
} }

View File

@ -1,7 +1,6 @@
package com.sismics.docs.adapter; package com.sismics.docs.adapter;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap;
import android.text.format.DateFormat; import android.text.format.DateFormat;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -10,9 +9,8 @@ import android.widget.BaseAdapter;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import com.androidquery.AQuery;
import com.androidquery.callback.BitmapAjaxCallback;
import com.sismics.docs.R; import com.sismics.docs.R;
import com.sismics.docs.util.OkHttpUtil;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
@ -27,22 +25,23 @@ import java.util.List;
* @author bgamard. * @author bgamard.
*/ */
public class CommentListAdapter extends BaseAdapter { public class CommentListAdapter extends BaseAdapter {
/**
* AQuery.
*/
private AQuery aq;
/** /**
* Tags. * Tags.
*/ */
private List<JSONObject> commentList = new ArrayList<>(); private List<JSONObject> commentList = new ArrayList<>();
/**
* Context.
*/
private Context context;
/** /**
* Comment list adapter. * Comment list adapter.
* *
* @param commentsArray Comments * @param commentsArray Comments
*/ */
public CommentListAdapter(JSONArray commentsArray) { public CommentListAdapter(Context context, JSONArray commentsArray) {
this.context = context;
for (int i = 0; i < commentsArray.length(); i++) { for (int i = 0; i < commentsArray.length(); i++) {
commentList.add(commentsArray.optJSONObject(i)); commentList.add(commentsArray.optJSONObject(i));
} }
@ -70,12 +69,6 @@ public class CommentListAdapter extends BaseAdapter {
view = vi.inflate(R.layout.comment_list_item, parent, false); view = vi.inflate(R.layout.comment_list_item, parent, false);
} }
if (aq == null) {
aq = new AQuery(view);
} else {
aq.recycle(view);
}
// Fill the view // Fill the view
JSONObject comment = getItem(position); JSONObject comment = getItem(position);
TextView creatorTextView = (TextView) view.findViewById(R.id.creatorTextView); TextView creatorTextView = (TextView) view.findViewById(R.id.creatorTextView);
@ -88,14 +81,9 @@ public class CommentListAdapter extends BaseAdapter {
// Gravatar image // Gravatar image
String gravatarUrl = "http://www.gravatar.com/avatar/" + comment.optString("creator_gravatar") + "?s=128d=identicon"; String gravatarUrl = "http://www.gravatar.com/avatar/" + comment.optString("creator_gravatar") + "?s=128d=identicon";
if (aq.shouldDelay(position, view, parent, gravatarUrl)) { OkHttpUtil.picasso(context, null)
aq.id(gravatarImageView).image((Bitmap) null); .load(gravatarUrl)
} else { .into(gravatarImageView);
aq.id(gravatarImageView).image(new BitmapAjaxCallback()
.url(gravatarUrl)
.animation(AQuery.FADE_IN_NETWORK)
);
}
return view; return view;
} }

View File

@ -7,10 +7,11 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import com.androidquery.AQuery;
import com.androidquery.callback.BitmapAjaxCallback;
import com.sismics.docs.R; import com.sismics.docs.R;
import com.sismics.docs.util.OkHttpUtil;
import com.sismics.docs.util.PreferenceUtil; import com.sismics.docs.util.PreferenceUtil;
import com.squareup.picasso.Callback;
import com.squareup.picasso.MemoryPolicy;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
@ -30,11 +31,6 @@ public class FilePagerAdapter extends PagerAdapter {
*/ */
private List<JSONObject> files; private List<JSONObject> files;
/**
* AQuery.
*/
private AQuery aq;
/** /**
* Context. * Context.
*/ */
@ -58,7 +54,6 @@ public class FilePagerAdapter extends PagerAdapter {
} }
this.context = context; this.context = context;
this.authToken = PreferenceUtil.getAuthToken(context); this.authToken = PreferenceUtil.getAuthToken(context);
aq = new AQuery(context);
} }
@Override @Override
@ -66,15 +61,20 @@ public class FilePagerAdapter extends PagerAdapter {
View view = LayoutInflater.from(container.getContext()).inflate(R.layout.file_viewpager_item, container, false); View view = LayoutInflater.from(container.getContext()).inflate(R.layout.file_viewpager_item, container, false);
ImageViewTouch fileImageView = (ImageViewTouch) view.findViewById(R.id.fileImageView); ImageViewTouch fileImageView = (ImageViewTouch) view.findViewById(R.id.fileImageView);
ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.fileProgressBar); final ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.fileProgressBar);
JSONObject file = files.get(position); JSONObject file = files.get(position);
String fileUrl = PreferenceUtil.getServerUrl(context) + "/api/file/" + file.optString("id") + "/data?size=web"; String fileUrl = PreferenceUtil.getServerUrl(context) + "/api/file/" + file.optString("id") + "/data?size=web";
aq.id(fileImageView)
.image(new BitmapAjaxCallback() // Load image
.url(fileUrl) OkHttpUtil.picasso(context, authToken)
.progress(progressBar) .load(fileUrl)
.animation(AQuery.FADE_IN_NETWORK) .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
.cookie("auth_token", authToken)); .into(fileImageView, new Callback.EmptyCallback() {
@Override
public void onSuccess() {
progressBar.setVisibility(View.GONE);
}
});
fileImageView.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN); fileImageView.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);

View File

@ -9,10 +9,10 @@ import android.preference.PreferenceManager;
import android.provider.SearchRecentSuggestions; import android.provider.SearchRecentSuggestions;
import android.widget.Toast; import android.widget.Toast;
import com.androidquery.util.AQUtility;
import com.sismics.docs.R; import com.sismics.docs.R;
import com.sismics.docs.provider.RecentSuggestionsProvider; import com.sismics.docs.provider.RecentSuggestionsProvider;
import com.sismics.docs.util.ApplicationUtil; import com.sismics.docs.util.ApplicationUtil;
import com.sismics.docs.util.OkHttpUtil;
import com.sismics.docs.util.PreferenceUtil; import com.sismics.docs.util.PreferenceUtil;
/** /**
@ -52,7 +52,7 @@ public class SettingsFragment extends PreferenceFragment implements SharedPrefer
clearCachePref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { clearCachePref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override @Override
public boolean onPreferenceClick(Preference preference) { public boolean onPreferenceClick(Preference preference) {
AQUtility.cleanCacheAsync(getActivity()); OkHttpUtil.clearCache(getActivity());
Toast.makeText(getActivity(), R.string.pref_clear_cache_success, Toast.LENGTH_LONG).show(); Toast.makeText(getActivity(), R.string.pref_clear_cache_success, Toast.LENGTH_LONG).show();
return true; return true;
} }

View File

@ -3,14 +3,11 @@ package com.sismics.docs.resource;
import android.content.Context; import android.content.Context;
import android.os.Build; import android.os.Build;
import com.androidquery.callback.AbstractAjaxCallback;
import com.loopj.android.http.AsyncHttpClient; import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.PersistentCookieStore; import com.loopj.android.http.PersistentCookieStore;
import com.sismics.docs.util.ApplicationUtil; import com.sismics.docs.util.ApplicationUtil;
import com.sismics.docs.util.PreferenceUtil; import com.sismics.docs.util.PreferenceUtil;
import org.apache.http.conn.ssl.SSLSocketFactory;
import java.io.IOException; import java.io.IOException;
import java.net.Socket; import java.net.Socket;
import java.security.KeyManagementException; import java.security.KeyManagementException;
@ -26,6 +23,8 @@ import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager; import javax.net.ssl.X509TrustManager;
import cz.msebera.android.httpclient.conn.ssl.SSLSocketFactory;
/** /**
* Base class for API access. * Base class for API access.
* *
@ -55,15 +54,10 @@ public class BaseResource {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null); trustStore.load(null, null);
// AQuery support only the old Apache HTTP library // Async HTTP Client uses another HTTP libary
MySSLSocketFactory sf = new MySSLSocketFactory(trustStore); MySSLSocketFactory sf = new MySSLSocketFactory(trustStore);
sf.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); sf.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
AbstractAjaxCallback.setSSF(sf); client.setSSLSocketFactory(sf);
// Async HTTP Client uses another HTTP libary
MySSLSocketFactory2 sf2 = new MySSLSocketFactory2(trustStore);
sf2.setHostnameVerifier(MySSLSocketFactory2.ALLOW_ALL_HOSTNAME_VERIFIER);
client.setSSLSocketFactory(sf2);
} catch (Exception e) { } catch (Exception e) {
// NOP // NOP
} }
@ -89,52 +83,15 @@ public class BaseResource {
} }
} }
/**
* Socket factory to allow self-signed certificates for AQuery.
*
* @author bgamard
*/
public static class MySSLSocketFactory extends SSLSocketFactory {
SSLContext sslContext = SSLContext.getInstance("TLS");
public MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
super(truststore);
TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
sslContext.init(null, new TrustManager[] { tm }, null);
}
@Override
public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException {
return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
}
@Override
public Socket createSocket() throws IOException {
return sslContext.getSocketFactory().createSocket();
}
}
/** /**
* Socket factory to allow self-signed certificates for Async HTTP Client. * Socket factory to allow self-signed certificates for Async HTTP Client.
* *
* @author bgamard * @author bgamard
*/ */
public static class MySSLSocketFactory2 extends cz.msebera.android.httpclient.conn.ssl.SSLSocketFactory { public static class MySSLSocketFactory extends cz.msebera.android.httpclient.conn.ssl.SSLSocketFactory {
SSLContext sslContext = SSLContext.getInstance("TLS"); SSLContext sslContext = SSLContext.getInstance(SSLSocketFactory.TLS);
public MySSLSocketFactory2(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException { public MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
super(truststore); super(truststore);
TrustManager tm = new X509TrustManager() { TrustManager tm = new X509TrustManager() {

View File

@ -58,8 +58,8 @@ public class FileResource extends BaseResource {
SyncHttpClient client = new SyncHttpClient(); SyncHttpClient client = new SyncHttpClient();
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null); trustStore.load(null, null);
MySSLSocketFactory2 sf = new MySSLSocketFactory2(trustStore); MySSLSocketFactory sf = new MySSLSocketFactory(trustStore);
sf.setHostnameVerifier(MySSLSocketFactory2.ALLOW_ALL_HOSTNAME_VERIFIER); sf.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
client.setSSLSocketFactory(sf); client.setSSLSocketFactory(sf);
client.setCookieStore(new PersistentCookieStore(context)); client.setCookieStore(new PersistentCookieStore(context));
client.setUserAgent(USER_AGENT); client.setUserAgent(USER_AGENT);

View File

@ -0,0 +1,123 @@
package com.sismics.docs.util;
import android.content.Context;
import android.util.Log;
import com.jakewharton.picasso.OkHttp3Downloader;
import com.squareup.picasso.Picasso;
import java.io.IOException;
import java.security.cert.CertificateException;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* Utilities for OkHttp.
*
* @author bgamard.
*/
public class OkHttpUtil {
/**
* Build a Picasso object with base config.
*
* @param context Context
* @param authToken Auth token
* @return Picasso object
*/
public static Picasso picasso(Context context, final String authToken) {
OkHttpClient okHttpClient = buildOkHttpClient()
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
final Request original = chain.request();
final Request.Builder requestBuilder = original.newBuilder()
.header("Cookie", "auth_token=" + authToken)
.header("Cache-Control", "max-age=" + (3600 * 24 * 365))
.method(original.method(), original.body());
return chain.proceed(requestBuilder.build());
}
})
.cache(new Cache(context.getCacheDir(),
PreferenceUtil.getIntegerPreference(context, PreferenceUtil.PREF_CACHE_SIZE, 0) * 1000000))
.build();
Picasso picasso = new Picasso.Builder(context)
.downloader(new OkHttp3Downloader(okHttpClient))
.build();
picasso.setIndicatorsEnabled(false);
return picasso;
}
/**
* Clear the cache.
*
* @param context Context
*/
public static void clearCache(Context context) {
Cache cache = new Cache(context.getCacheDir(),
PreferenceUtil.getIntegerPreference(context, PreferenceUtil.PREF_CACHE_SIZE, Integer.MAX_VALUE));
try {
cache.evictAll();
} catch (IOException e) {
Log.e("OKHttpUtil", "Error clearing cache", e);
}
}
/**
* Build a OkHttpClient accepting all SSL certificates.
*
* @return OkHttpClient.Builder
*/
private static OkHttpClient.Builder buildOkHttpClient() {
try {
// Create a trust manager that does not validate certificate chains
final TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
}
};
// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
// Create an ssl socket factory with our all-trusting manager
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.sslSocketFactory(sslSocketFactory);
builder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
return builder;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

View File

@ -9,7 +9,7 @@
android:layout_width="200dp" android:layout_width="200dp"
android:layout_height="15dip" android:layout_height="15dip"
android:id="@+id/fileProgressBar" android:id="@+id/fileProgressBar"
android:indeterminate="false" android:indeterminate="true"
android:layout_centerInParent="true"/> android:layout_centerInParent="true"/>
<it.sephiroth.android.library.imagezoom.ImageViewTouch <it.sephiroth.android.library.imagezoom.ImageViewTouch

View File

@ -1,6 +1,6 @@
#Mon Nov 23 20:12:30 CET 2015 #Sat Jan 16 19:15:13 CET 2016
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip