updated .gitignore

This commit is contained in:
Mariotaku Lee 2014-09-20 13:33:27 +08:00
parent 25464da78c
commit 4eb84d8bd5
4 changed files with 499 additions and 471 deletions

30
.gitignore vendored
View File

@ -1,6 +1,28 @@
.gradle # Built application files
/local.properties
/.idea/workspace.xml
.DS_Store
/build /build
# Local configuration file (sdk path, etc)
local.properties
# Gradle generated files
/gradle
.gradle/
# Signing files
.signing/
# User-specific configurations
/.idea
*.iml
# OS-specific files
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
# Private files
/signing.properties /signing.properties

View File

@ -1,20 +1,21 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>Google Maps</title> <title>Google Maps</title>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" user-scalable="no"/> <meta name="viewport" user-scalable="no"/>
<style type="text/css"> <style type="text/css">
html, body, #map_canvas { html, body, #map_canvas {
background-image: url('images/loading_tile.png'); background-image: url('images/loading_tile.png');
margin: 0; margin: 0;
padding: 0; padding: 0;
width: 100%; width: 100%;
height: 100%; height: 100%;
} }
</style> </style>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=true"></script> <script type="text/javascript"
<script type="text/javascript"> src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>
<script type="text/javascript">
var map; var map;
function initialize() { function initialize() {
var latitude = 0; var latitude = 0;
@ -67,9 +68,10 @@
} }
google.maps.event.addDomListener(window, 'load', initialize); google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head> </script>
<body> </head>
<div id="map_canvas"></div> <body>
</body> <div id="map_canvas"></div>
</body>
</html> </html>

View File

@ -51,258 +51,259 @@ import java.io.OutputStream;
public class GLImageLoader extends AsyncTaskLoader<GLImageLoader.Result> implements Constants { public class GLImageLoader extends AsyncTaskLoader<GLImageLoader.Result> implements Constants {
private final Uri mUri; private final Uri mUri;
private final Handler mHandler; private final Handler mHandler;
private final DownloadListener mListener; private final DownloadListener mListener;
private final ImageDownloader mDownloader; private final ImageDownloader mDownloader;
private final DiskCache mDiskCache; private final DiskCache mDiskCache;
private final float mFallbackSize; private final float mFallbackSize;
private final long mAccountId; private final long mAccountId;
public GLImageLoader(final Context context, final DownloadListener listener, final long accountId, final Uri uri) { public GLImageLoader(final Context context, final DownloadListener listener, final long accountId, final Uri uri) {
super(context); super(context);
mHandler = new Handler(); mHandler = new Handler();
mAccountId = accountId; mAccountId = accountId;
mUri = uri; mUri = uri;
mListener = listener; mListener = listener;
final TwidereApplication app = TwidereApplication.getInstance(context); final TwidereApplication app = TwidereApplication.getInstance(context);
mDownloader = app.getFullImageDownloader(); mDownloader = app.getFullImageDownloader();
mDiskCache = app.getFullDiskCache(); mDiskCache = app.getFullDiskCache();
final Resources res = context.getResources(); final Resources res = context.getResources();
final DisplayMetrics dm = res.getDisplayMetrics(); final DisplayMetrics dm = res.getDisplayMetrics();
mFallbackSize = Math.max(dm.heightPixels, dm.widthPixels); mFallbackSize = Math.max(dm.heightPixels, dm.widthPixels);
} }
@Override @Override
public GLImageLoader.Result loadInBackground() { public GLImageLoader.Result loadInBackground() {
if (mUri == null) { if (mUri == null) {
Result.nullInstance(); return Result.nullInstance();
} }
final String scheme = mUri.getScheme(); final String scheme = mUri.getScheme();
if ("http".equals(scheme) || "https".equals(scheme)) { if ("http".equals(scheme) || "https".equals(scheme)) {
final String url = ParseUtils.parseString(mUri.toString()); final String url = ParseUtils.parseString(mUri.toString());
if (url == null) return Result.nullInstance(); if (url == null) return Result.nullInstance();
final File cacheFile = mDiskCache.get(url); final File cacheFile = mDiskCache.get(url);
if (cacheFile != null) { if (cacheFile != null) {
final File cacheDir = cacheFile.getParentFile(); final File cacheDir = cacheFile.getParentFile();
if (cacheDir != null && !cacheDir.exists()) { if (cacheDir != null && !cacheDir.exists()) {
cacheDir.mkdirs(); cacheDir.mkdirs();
} }
} else } else
return Result.nullInstance(); return Result.nullInstance();
try { try {
// from SD cache // from SD cache
if (ImageValidator.checkImageValidity(cacheFile)) return decodeImageInternal(cacheFile); if (ImageValidator.checkImageValidity(cacheFile))
return decodeImageInternal(cacheFile);
final InputStream is = mDownloader.getStream(url, new AccountExtra(mAccountId)); final InputStream is = mDownloader.getStream(url, new AccountExtra(mAccountId));
if (is == null) return Result.nullInstance(); if (is == null) return Result.nullInstance();
final long length = is.available(); final long length = is.available();
mHandler.post(new DownloadStartRunnable(this, mListener, length)); mHandler.post(new DownloadStartRunnable(this, mListener, length));
final OutputStream os = new FileOutputStream(cacheFile); final OutputStream os = new FileOutputStream(cacheFile);
try { try {
dump(is, os); dump(is, os);
mHandler.post(new DownloadFinishRunnable(this, mListener)); mHandler.post(new DownloadFinishRunnable(this, mListener));
} finally { } finally {
GalleryUtils.closeSilently(is); GalleryUtils.closeSilently(is);
GalleryUtils.closeSilently(os); GalleryUtils.closeSilently(os);
} }
if (!ImageValidator.checkImageValidity(cacheFile)) { if (!ImageValidator.checkImageValidity(cacheFile)) {
// The file is corrupted, so we remove it from // The file is corrupted, so we remove it from
// cache. // cache.
final Result result = decodeBitmapOnly(cacheFile); final Result result = decodeBitmapOnly(cacheFile);
if (cacheFile.isFile()) { if (cacheFile.isFile()) {
cacheFile.delete(); cacheFile.delete();
} }
return result; return result;
} }
return decodeImageInternal(cacheFile); return decodeImageInternal(cacheFile);
} catch (final Exception e) { } catch (final Exception e) {
mHandler.post(new DownloadErrorRunnable(this, mListener, e)); mHandler.post(new DownloadErrorRunnable(this, mListener, e));
return Result.getInstance(cacheFile, e); return Result.getInstance(cacheFile, e);
} }
} else if (ContentResolver.SCHEME_FILE.equals(scheme)) { } else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
final File file = new File(mUri.getPath()); final File file = new File(mUri.getPath());
try { try {
return decodeImage(file); return decodeImage(file);
} catch (final Exception e) { } catch (final Exception e) {
return Result.getInstance(file, e); return Result.getInstance(file, e);
} }
} }
return Result.nullInstance(); return Result.nullInstance();
} }
protected Result decodeBitmapOnly(final File file) { protected Result decodeBitmapOnly(final File file) {
final String path = file.getAbsolutePath(); final String path = file.getAbsolutePath();
final BitmapFactory.Options o = new BitmapFactory.Options(); final BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true; o.inJustDecodeBounds = true;
o.inPreferredConfig = Bitmap.Config.RGB_565; o.inPreferredConfig = Bitmap.Config.RGB_565;
BitmapFactory.decodeFile(path, o); BitmapFactory.decodeFile(path, o);
final int width = o.outWidth, height = o.outHeight; final int width = o.outWidth, height = o.outHeight;
if (width <= 0 || height <= 0) return Result.getInstance(file, null); if (width <= 0 || height <= 0) return Result.getInstance(file, null);
o.inJustDecodeBounds = false; o.inJustDecodeBounds = false;
o.inSampleSize = BitmapUtils.computeSampleSize(mFallbackSize / Math.max(width, height)); o.inSampleSize = BitmapUtils.computeSampleSize(mFallbackSize / Math.max(width, height));
final Bitmap bitmap = BitmapFactory.decodeFile(path, o); final Bitmap bitmap = BitmapFactory.decodeFile(path, o);
return Result.getInstance(bitmap, Exif.getOrientation(file), file); return Result.getInstance(bitmap, Exif.getOrientation(file), file);
} }
protected Result decodeImage(final File file) { protected Result decodeImage(final File file) {
final String path = file.getAbsolutePath(); final String path = file.getAbsolutePath();
try { try {
final BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(path, false); final BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(path, false);
final int width = decoder.getWidth(); final int width = decoder.getWidth();
final int height = decoder.getHeight(); final int height = decoder.getHeight();
final BitmapFactory.Options options = new BitmapFactory.Options(); final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = BitmapUtils.computeSampleSize(mFallbackSize / Math.max(width, height)); options.inSampleSize = BitmapUtils.computeSampleSize(mFallbackSize / Math.max(width, height));
options.inPreferredConfig = Bitmap.Config.RGB_565; options.inPreferredConfig = Bitmap.Config.RGB_565;
final Bitmap bitmap = decoder.decodeRegion(new Rect(0, 0, width, height), options); final Bitmap bitmap = decoder.decodeRegion(new Rect(0, 0, width, height), options);
return Result.getInstance(decoder, bitmap, Exif.getOrientation(file), file); return Result.getInstance(decoder, bitmap, Exif.getOrientation(file), file);
} catch (final IOException e) { } catch (final IOException e) {
return decodeBitmapOnly(file); return decodeBitmapOnly(file);
} }
} }
@Override @Override
protected void onStartLoading() { protected void onStartLoading() {
forceLoad(); forceLoad();
} }
private Result decodeImageInternal(final File file) throws IOException { private Result decodeImageInternal(final File file) throws IOException {
if (ImageValidator.checkImageValidity(file)) return decodeImage(file); if (ImageValidator.checkImageValidity(file)) return decodeImage(file);
throw new InvalidImageException(); throw new InvalidImageException();
} }
private void dump(final InputStream is, final OutputStream os) throws IOException { private void dump(final InputStream is, final OutputStream os) throws IOException {
final byte buffer[] = new byte[1024]; final byte buffer[] = new byte[1024];
int rc = is.read(buffer, 0, buffer.length); int rc = is.read(buffer, 0, buffer.length);
long downloaded = 0; long downloaded = 0;
while (rc > 0) { while (rc > 0) {
downloaded += rc; downloaded += rc;
mHandler.post(new ProgressUpdateRunnable(mListener, downloaded)); mHandler.post(new ProgressUpdateRunnable(mListener, downloaded));
os.write(buffer, 0, rc); os.write(buffer, 0, rc);
rc = is.read(buffer, 0, buffer.length); rc = is.read(buffer, 0, buffer.length);
} }
} }
public static interface DownloadListener { public static interface DownloadListener {
void onDownloadError(Throwable t); void onDownloadError(Throwable t);
void onDownloadFinished(); void onDownloadFinished();
void onDownloadStart(long total); void onDownloadStart(long total);
void onProgressUpdate(long downloaded); void onProgressUpdate(long downloaded);
} }
public static class InvalidImageException extends IOException { public static class InvalidImageException extends IOException {
private static final long serialVersionUID = 8996099908714452289L; private static final long serialVersionUID = 8996099908714452289L;
} }
public static class Result { public static class Result {
public final Bitmap bitmap; public final Bitmap bitmap;
public final File file; public final File file;
public final Exception exception; public final Exception exception;
public final BitmapRegionDecoder decoder; public final BitmapRegionDecoder decoder;
public final int orientation; public final int orientation;
public Result(final BitmapRegionDecoder decoder, final Bitmap bitmap, final int orientation, final File file, public Result(final BitmapRegionDecoder decoder, final Bitmap bitmap, final int orientation, final File file,
final Exception exception) { final Exception exception) {
this.bitmap = bitmap; this.bitmap = bitmap;
this.file = file; this.file = file;
this.decoder = decoder; this.decoder = decoder;
this.orientation = orientation; this.orientation = orientation;
this.exception = exception; this.exception = exception;
} }
public static Result getInstance(final Bitmap bitmap, final int orientation, final File file) { public static Result getInstance(final Bitmap bitmap, final int orientation, final File file) {
return new Result(null, bitmap, orientation, file, null); return new Result(null, bitmap, orientation, file, null);
} }
public static Result getInstance(final BitmapRegionDecoder decoder, final Bitmap bitmap, final int orientation, public static Result getInstance(final BitmapRegionDecoder decoder, final Bitmap bitmap, final int orientation,
final File file) { final File file) {
return new Result(decoder, bitmap, orientation, file, null); return new Result(decoder, bitmap, orientation, file, null);
} }
public static Result getInstance(final File file, final Exception e) { public static Result getInstance(final File file, final Exception e) {
return new Result(null, null, 0, file, e); return new Result(null, null, 0, file, e);
} }
public static Result nullInstance() { public static Result nullInstance() {
return new Result(null, null, 0, null, null); return new Result(null, null, 0, null, null);
} }
} }
private final static class DownloadErrorRunnable implements Runnable { private final static class DownloadErrorRunnable implements Runnable {
private final GLImageLoader loader; private final GLImageLoader loader;
private final DownloadListener listener; private final DownloadListener listener;
private final Throwable t; private final Throwable t;
DownloadErrorRunnable(final GLImageLoader loader, final DownloadListener listener, final Throwable t) { DownloadErrorRunnable(final GLImageLoader loader, final DownloadListener listener, final Throwable t) {
this.loader = loader; this.loader = loader;
this.listener = listener; this.listener = listener;
this.t = t; this.t = t;
} }
@Override @Override
public void run() { public void run() {
if (listener == null || loader.isAbandoned() || loader.isReset()) return; if (listener == null || loader.isAbandoned() || loader.isReset()) return;
listener.onDownloadError(t); listener.onDownloadError(t);
} }
} }
private final static class DownloadFinishRunnable implements Runnable { private final static class DownloadFinishRunnable implements Runnable {
private final GLImageLoader loader; private final GLImageLoader loader;
private final DownloadListener listener; private final DownloadListener listener;
DownloadFinishRunnable(final GLImageLoader loader, final DownloadListener listener) { DownloadFinishRunnable(final GLImageLoader loader, final DownloadListener listener) {
this.loader = loader; this.loader = loader;
this.listener = listener; this.listener = listener;
} }
@Override @Override
public void run() { public void run() {
if (listener == null || loader.isAbandoned() || loader.isReset()) return; if (listener == null || loader.isAbandoned() || loader.isReset()) return;
listener.onDownloadFinished(); listener.onDownloadFinished();
} }
} }
private final static class DownloadStartRunnable implements Runnable { private final static class DownloadStartRunnable implements Runnable {
private final GLImageLoader loader; private final GLImageLoader loader;
private final DownloadListener listener; private final DownloadListener listener;
private final long total; private final long total;
DownloadStartRunnable(final GLImageLoader loader, final DownloadListener listener, final long total) { DownloadStartRunnable(final GLImageLoader loader, final DownloadListener listener, final long total) {
this.loader = loader; this.loader = loader;
this.listener = listener; this.listener = listener;
this.total = total; this.total = total;
} }
@Override @Override
public void run() { public void run() {
if (listener == null || loader.isAbandoned() || loader.isReset()) return; if (listener == null || loader.isAbandoned() || loader.isReset()) return;
listener.onDownloadStart(total); listener.onDownloadStart(total);
} }
} }
private final static class ProgressUpdateRunnable implements Runnable { private final static class ProgressUpdateRunnable implements Runnable {
private final DownloadListener listener; private final DownloadListener listener;
private final long current; private final long current;
ProgressUpdateRunnable(final DownloadListener listener, final long current) { ProgressUpdateRunnable(final DownloadListener listener, final long current) {
this.listener = listener; this.listener = listener;
this.current = current; this.current = current;
} }
@Override @Override
public void run() { public void run() {
if (listener == null) return; if (listener == null) return;
listener.onProgressUpdate(current); listener.onProgressUpdate(current);
} }
} }
} }

View File

@ -19,13 +19,6 @@
package org.mariotaku.twidere.app; package org.mariotaku.twidere.app;
import static org.mariotaku.twidere.util.UserColorNicknameUtils.initUserColor;
import static org.mariotaku.twidere.util.Utils.getBestCacheDir;
import static org.mariotaku.twidere.util.Utils.getInternalCacheDir;
import static org.mariotaku.twidere.util.Utils.initAccountColor;
import static org.mariotaku.twidere.util.Utils.startProfilingServiceIfNeeded;
import static org.mariotaku.twidere.util.Utils.startRefreshServiceIfNeeded;
import android.app.Application; import android.app.Application;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
@ -48,8 +41,6 @@ import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.ImageDownloader; import com.nostra13.universalimageloader.core.download.ImageDownloader;
import com.nostra13.universalimageloader.utils.L; import com.nostra13.universalimageloader.utils.L;
import edu.ucdavis.earlybird.UCDService;
import org.acra.ACRA; import org.acra.ACRA;
import org.acra.ReportField; import org.acra.ReportField;
import org.acra.annotation.ReportsCrashes; import org.acra.annotation.ReportsCrashes;
@ -75,269 +66,281 @@ import org.mariotaku.twidere.util.imageloader.TwidereImageDownloader;
import org.mariotaku.twidere.util.imageloader.URLFileNameGenerator; import org.mariotaku.twidere.util.imageloader.URLFileNameGenerator;
import org.mariotaku.twidere.util.net.TwidereHostAddressResolver; import org.mariotaku.twidere.util.net.TwidereHostAddressResolver;
import twitter4j.http.HostAddressResolver;
import java.io.File; import java.io.File;
import java.util.Date; import java.util.Date;
import java.util.Locale; import java.util.Locale;
import edu.ucdavis.earlybird.UCDService;
import twitter4j.http.HostAddressResolver;
import static org.mariotaku.twidere.util.UserColorNicknameUtils.initUserColor;
import static org.mariotaku.twidere.util.Utils.getBestCacheDir;
import static org.mariotaku.twidere.util.Utils.getInternalCacheDir;
import static org.mariotaku.twidere.util.Utils.initAccountColor;
import static org.mariotaku.twidere.util.Utils.startProfilingServiceIfNeeded;
import static org.mariotaku.twidere.util.Utils.startRefreshServiceIfNeeded;
@ReportsCrashes(formKey = "", mailTo = Constants.APP_PROJECT_EMAIL, sharedPreferencesMode = Context.MODE_PRIVATE, @ReportsCrashes(formKey = "", mailTo = Constants.APP_PROJECT_EMAIL, sharedPreferencesMode = Context.MODE_PRIVATE,
sharedPreferencesName = Constants.SHARED_PREFERENCES_NAME) sharedPreferencesName = Constants.SHARED_PREFERENCES_NAME)
public class TwidereApplication extends Application implements Constants, OnSharedPreferenceChangeListener { public class TwidereApplication extends Application implements Constants, OnSharedPreferenceChangeListener {
private Handler mHandler; private Handler mHandler;
private ImageLoaderWrapper mImageLoaderWrapper; private ImageLoaderWrapper mImageLoaderWrapper;
private ImageLoader mImageLoader; private ImageLoader mImageLoader;
private AsyncTaskManager mAsyncTaskManager; private AsyncTaskManager mAsyncTaskManager;
private SharedPreferences mPreferences; private SharedPreferences mPreferences;
private AsyncTwitterWrapper mTwitterWrapper; private AsyncTwitterWrapper mTwitterWrapper;
private MultiSelectManager mMultiSelectManager; private MultiSelectManager mMultiSelectManager;
private TwidereImageDownloader mImageDownloader, mFullImageDownloader; private TwidereImageDownloader mImageDownloader, mFullImageDownloader;
private DiskCache mDiskCache, mFullDiskCache; private DiskCache mDiskCache, mFullDiskCache;
private MessagesManager mCroutonsManager; private MessagesManager mCroutonsManager;
private SQLiteOpenHelper mSQLiteOpenHelper; private SQLiteOpenHelper mSQLiteOpenHelper;
private SwipebackScreenshotManager mSwipebackScreenshotManager; private SwipebackScreenshotManager mSwipebackScreenshotManager;
private HostAddressResolver mResolver; private HostAddressResolver mResolver;
private SQLiteDatabase mDatabase; private SQLiteDatabase mDatabase;
public AsyncTaskManager getAsyncTaskManager() { public AsyncTaskManager getAsyncTaskManager() {
if (mAsyncTaskManager != null) return mAsyncTaskManager; if (mAsyncTaskManager != null) return mAsyncTaskManager;
return mAsyncTaskManager = AsyncTaskManager.getInstance(); return mAsyncTaskManager = AsyncTaskManager.getInstance();
} }
public DiskCache getDiskCache() { public DiskCache getDiskCache() {
if (mDiskCache != null) return mDiskCache; if (mDiskCache != null) return mDiskCache;
return mDiskCache = getDiskCache(DIR_NAME_IMAGE_CACHE); return mDiskCache = createDiskCache(DIR_NAME_IMAGE_CACHE);
} }
public DiskCache getFullDiskCache() { public DiskCache getFullDiskCache() {
if (mFullDiskCache != null) return mFullDiskCache; if (mFullDiskCache != null) return mFullDiskCache;
return mFullDiskCache = getDiskCache(DIR_NAME_FULL_IMAGE_CACHE); return mFullDiskCache = createDiskCache(DIR_NAME_FULL_IMAGE_CACHE);
} }
public ImageDownloader getFullImageDownloader() { public ImageDownloader getFullImageDownloader() {
if (mFullImageDownloader != null) return mFullImageDownloader; if (mFullImageDownloader != null) return mFullImageDownloader;
return mFullImageDownloader = new TwidereImageDownloader(this, true); return mFullImageDownloader = new TwidereImageDownloader(this, true);
} }
public Handler getHandler() { public Handler getHandler() {
return mHandler; return mHandler;
} }
public HostAddressResolver getHostAddressResolver() { public HostAddressResolver getHostAddressResolver() {
if (mResolver != null) return mResolver; if (mResolver != null) return mResolver;
return mResolver = new TwidereHostAddressResolver(this); return mResolver = new TwidereHostAddressResolver(this);
} }
public ImageDownloader getImageDownloader() { public ImageDownloader getImageDownloader() {
if (mImageDownloader != null) return mImageDownloader; if (mImageDownloader != null) return mImageDownloader;
return mImageDownloader = new TwidereImageDownloader(this, false); return mImageDownloader = new TwidereImageDownloader(this, false);
} }
public ImageLoader getImageLoader() { public ImageLoader getImageLoader() {
if (mImageLoader != null) return mImageLoader; if (mImageLoader != null) return mImageLoader;
final ImageLoader loader = ImageLoader.getInstance(); final ImageLoader loader = ImageLoader.getInstance();
final ImageLoaderConfiguration.Builder cb = new ImageLoaderConfiguration.Builder(this); final ImageLoaderConfiguration.Builder cb = new ImageLoaderConfiguration.Builder(this);
cb.threadPriority(Thread.NORM_PRIORITY - 2); cb.threadPriority(Thread.NORM_PRIORITY - 2);
cb.denyCacheImageMultipleSizesInMemory(); cb.denyCacheImageMultipleSizesInMemory();
cb.tasksProcessingOrder(QueueProcessingType.LIFO); cb.tasksProcessingOrder(QueueProcessingType.LIFO);
// cb.memoryCache(new ImageMemoryCache(40)); // cb.memoryCache(new ImageMemoryCache(40));
cb.diskCache(getDiskCache()); cb.diskCache(getDiskCache());
cb.imageDownloader(getImageDownloader()); cb.imageDownloader(getImageDownloader());
L.writeDebugLogs(Utils.isDebugBuild()); L.writeDebugLogs(Utils.isDebugBuild());
loader.init(cb.build()); loader.init(cb.build());
return mImageLoader = loader; return mImageLoader = loader;
} }
public ImageLoaderWrapper getImageLoaderWrapper() { public ImageLoaderWrapper getImageLoaderWrapper() {
if (mImageLoaderWrapper != null) return mImageLoaderWrapper; if (mImageLoaderWrapper != null) return mImageLoaderWrapper;
return mImageLoaderWrapper = new ImageLoaderWrapper(getImageLoader()); return mImageLoaderWrapper = new ImageLoaderWrapper(getImageLoader());
} }
public MessagesManager getMessagesManager() { public MessagesManager getMessagesManager() {
if (mCroutonsManager != null) return mCroutonsManager; if (mCroutonsManager != null) return mCroutonsManager;
return mCroutonsManager = new MessagesManager(this); return mCroutonsManager = new MessagesManager(this);
} }
public MultiSelectManager getMultiSelectManager() { public MultiSelectManager getMultiSelectManager() {
if (mMultiSelectManager != null) return mMultiSelectManager; if (mMultiSelectManager != null) return mMultiSelectManager;
return mMultiSelectManager = new MultiSelectManager(); return mMultiSelectManager = new MultiSelectManager();
} }
public SQLiteDatabase getSQLiteDatabase() { public SQLiteDatabase getSQLiteDatabase() {
if (mDatabase != null) return mDatabase; if (mDatabase != null) return mDatabase;
StrictModeUtils.checkDiskIO(); StrictModeUtils.checkDiskIO();
return mDatabase = getSQLiteOpenHelper().getWritableDatabase(); return mDatabase = getSQLiteOpenHelper().getWritableDatabase();
} }
public SQLiteOpenHelper getSQLiteOpenHelper() { public SQLiteOpenHelper getSQLiteOpenHelper() {
if (mSQLiteOpenHelper != null) return mSQLiteOpenHelper; if (mSQLiteOpenHelper != null) return mSQLiteOpenHelper;
return mSQLiteOpenHelper = new TwidereSQLiteOpenHelper(this, DATABASES_NAME, DATABASES_VERSION); return mSQLiteOpenHelper = new TwidereSQLiteOpenHelper(this, DATABASES_NAME, DATABASES_VERSION);
} }
public SwipebackScreenshotManager getSwipebackScreenshotManager() { public SwipebackScreenshotManager getSwipebackScreenshotManager() {
if (mSwipebackScreenshotManager != null) return mSwipebackScreenshotManager; if (mSwipebackScreenshotManager != null) return mSwipebackScreenshotManager;
return mSwipebackScreenshotManager = new SwipebackScreenshotManager(this); return mSwipebackScreenshotManager = new SwipebackScreenshotManager(this);
} }
public AsyncTwitterWrapper getTwitterWrapper() { public AsyncTwitterWrapper getTwitterWrapper() {
if (mTwitterWrapper != null) return mTwitterWrapper; if (mTwitterWrapper != null) return mTwitterWrapper;
return mTwitterWrapper = AsyncTwitterWrapper.getInstance(this); return mTwitterWrapper = AsyncTwitterWrapper.getInstance(this);
} }
@Override @Override
public void onCreate() { public void onCreate() {
if (Utils.isDebugBuild()) { if (Utils.isDebugBuild()) {
StrictModeUtils.detectAllVmPolicy(); StrictModeUtils.detectAllVmPolicy();
} }
super.onCreate(); super.onCreate();
mPreferences = getSharedPreferences(SHARED_PREFERENCES_NAME, MODE_PRIVATE); mPreferences = getSharedPreferences(SHARED_PREFERENCES_NAME, MODE_PRIVATE);
mHandler = new Handler(); mHandler = new Handler();
mPreferences.registerOnSharedPreferenceChangeListener(this); mPreferences.registerOnSharedPreferenceChangeListener(this);
configACRA(); configACRA();
initializeAsyncTask(); initializeAsyncTask();
GalleryUtils.initialize(this); GalleryUtils.initialize(this);
initAccountColor(this); initAccountColor(this);
initUserColor(this); initUserColor(this);
final PackageManager pm = getPackageManager(); final PackageManager pm = getPackageManager();
final ComponentName main = new ComponentName(this, MainActivity.class); final ComponentName main = new ComponentName(this, MainActivity.class);
final ComponentName main2 = new ComponentName(this, MainHondaJOJOActivity.class); final ComponentName main2 = new ComponentName(this, MainHondaJOJOActivity.class);
final boolean mainDisabled = pm.getComponentEnabledSetting(main) != PackageManager.COMPONENT_ENABLED_STATE_ENABLED; final boolean mainDisabled = pm.getComponentEnabledSetting(main) != PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
final boolean main2Disabled = pm.getComponentEnabledSetting(main2) != PackageManager.COMPONENT_ENABLED_STATE_ENABLED; final boolean main2Disabled = pm.getComponentEnabledSetting(main2) != PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
final boolean no_entry = mainDisabled && main2Disabled; final boolean no_entry = mainDisabled && main2Disabled;
if (no_entry) { if (no_entry) {
pm.setComponentEnabledSetting(main, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, pm.setComponentEnabledSetting(main, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP); PackageManager.DONT_KILL_APP);
} else if (!mainDisabled) { } else if (!mainDisabled) {
pm.setComponentEnabledSetting(main2, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, pm.setComponentEnabledSetting(main2, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP); PackageManager.DONT_KILL_APP);
} }
startProfilingServiceIfNeeded(this); startProfilingServiceIfNeeded(this);
startRefreshServiceIfNeeded(this); startRefreshServiceIfNeeded(this);
} }
@Override @Override
public void onLowMemory() { public void onLowMemory() {
if (mImageLoaderWrapper != null) { if (mImageLoaderWrapper != null) {
mImageLoaderWrapper.clearMemoryCache(); mImageLoaderWrapper.clearMemoryCache();
} }
super.onLowMemory(); super.onLowMemory();
} }
@Override @Override
public void onSharedPreferenceChanged(final SharedPreferences preferences, final String key) { public void onSharedPreferenceChanged(final SharedPreferences preferences, final String key) {
if (KEY_REFRESH_INTERVAL.equals(key)) { if (KEY_REFRESH_INTERVAL.equals(key)) {
stopService(new Intent(this, RefreshService.class)); stopService(new Intent(this, RefreshService.class));
startRefreshServiceIfNeeded(this); startRefreshServiceIfNeeded(this);
} else if (KEY_ENABLE_PROXY.equals(key) || KEY_CONNECTION_TIMEOUT.equals(key) || KEY_PROXY_HOST.equals(key) } else if (KEY_ENABLE_PROXY.equals(key) || KEY_CONNECTION_TIMEOUT.equals(key) || KEY_PROXY_HOST.equals(key)
|| KEY_PROXY_PORT.equals(key) || KEY_FAST_IMAGE_LOADING.equals(key)) { || KEY_PROXY_PORT.equals(key) || KEY_FAST_IMAGE_LOADING.equals(key)) {
reloadConnectivitySettings(); reloadConnectivitySettings();
} else if (KEY_UCD_DATA_PROFILING.equals(key)) { } else if (KEY_UCD_DATA_PROFILING.equals(key)) {
stopService(new Intent(this, UCDService.class)); stopService(new Intent(this, UCDService.class));
startProfilingServiceIfNeeded(this); startProfilingServiceIfNeeded(this);
} else if (KEY_CONSUMER_KEY.equals(key) || KEY_CONSUMER_SECRET.equals(key) || KEY_API_URL_FORMAT.equals(key) } else if (KEY_CONSUMER_KEY.equals(key) || KEY_CONSUMER_SECRET.equals(key) || KEY_API_URL_FORMAT.equals(key)
|| KEY_AUTH_TYPE.equals(key) || KEY_SAME_OAUTH_SIGNING_URL.equals(key)) { || KEY_AUTH_TYPE.equals(key) || KEY_SAME_OAUTH_SIGNING_URL.equals(key)) {
final SharedPreferences.Editor editor = preferences.edit(); final SharedPreferences.Editor editor = preferences.edit();
editor.putLong(KEY_API_LAST_CHANGE, System.currentTimeMillis()); editor.putLong(KEY_API_LAST_CHANGE, System.currentTimeMillis());
editor.apply(); editor.apply();
} }
} }
public void reloadConnectivitySettings() { public void reloadConnectivitySettings() {
if (mImageDownloader != null) { if (mImageDownloader != null) {
mImageDownloader.reloadConnectivitySettings(); mImageDownloader.reloadConnectivitySettings();
} }
} }
private void configACRA() { private void configACRA() {
ACRA.init(this); ACRA.init(this);
ACRA.getErrorReporter().setReportSender(new EmailIntentSender(this)); ACRA.getErrorReporter().setReportSender(new EmailIntentSender(this));
} }
private DiskCache getDiskCache(final String dirName) { private DiskCache createDiskCache(final String dirName) {
final File cacheDir = getBestCacheDir(this, dirName); final File cacheDir = getBestCacheDir(this, dirName);
final File fallbackCacheDir = getInternalCacheDir(this, dirName); final File fallbackCacheDir = getInternalCacheDir(this, dirName);
return new UnlimitedDiscCache(cacheDir, fallbackCacheDir, new URLFileNameGenerator()); // final LruDiscCache discCache = new LruDiscCache(cacheDir, new URLFileNameGenerator(), 384 *
} // 1024 * 1024);
// discCache.setReserveCacheDir(fallbackCacheDir);
// return discCache;
return new UnlimitedDiscCache(cacheDir, fallbackCacheDir, new URLFileNameGenerator());
}
private void initializeAsyncTask() { private void initializeAsyncTask() {
// AsyncTask class needs to be loaded in UI thread. // AsyncTask class needs to be loaded in UI thread.
// So we load it here to comply the rule. // So we load it here to comply the rule.
try { try {
Class.forName(AsyncTask.class.getName()); Class.forName(AsyncTask.class.getName());
} catch (final ClassNotFoundException e) { } catch (final ClassNotFoundException e) {
} }
} }
public static TwidereApplication getInstance(final Context context) { public static TwidereApplication getInstance(final Context context) {
if (context == null) return null; if (context == null) return null;
final Context app = context.getApplicationContext(); final Context app = context.getApplicationContext();
return app instanceof TwidereApplication ? (TwidereApplication) app : null; return app instanceof TwidereApplication ? (TwidereApplication) app : null;
} }
static class EmailIntentSender implements ReportSender { static class EmailIntentSender implements ReportSender {
private final Context mContext; private final Context mContext;
EmailIntentSender(final Context ctx) { EmailIntentSender(final Context ctx) {
mContext = ctx; mContext = ctx;
} }
@Override @Override
public void send(final CrashReportData errorContent) throws ReportSenderException { public void send(final CrashReportData errorContent) throws ReportSenderException {
final Intent email = new Intent(Intent.ACTION_SEND); final Intent email = new Intent(Intent.ACTION_SEND);
email.setType("text/plain"); email.setType("text/plain");
email.putExtra(Intent.EXTRA_SUBJECT, String.format("%s Crash Report", getAppName())); email.putExtra(Intent.EXTRA_SUBJECT, String.format("%s Crash Report", getAppName()));
email.putExtra(Intent.EXTRA_TEXT, buildBody(errorContent)); email.putExtra(Intent.EXTRA_TEXT, buildBody(errorContent));
email.putExtra(Intent.EXTRA_EMAIL, new String[] { APP_PROJECT_EMAIL }); email.putExtra(Intent.EXTRA_EMAIL, new String[]{APP_PROJECT_EMAIL});
final Intent chooser = Intent.createChooser(email, mContext.getString(R.string.send_crash_report)); final Intent chooser = Intent.createChooser(email, mContext.getString(R.string.send_crash_report));
chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(chooser); mContext.startActivity(chooser);
} }
private String buildBody(final CrashReportData errorContent) { private String buildBody(final CrashReportData errorContent) {
final String stack_trace = errorContent.getProperty(ReportField.STACK_TRACE); final String stack_trace = errorContent.getProperty(ReportField.STACK_TRACE);
final StringBuilder builder = new StringBuilder(); final StringBuilder builder = new StringBuilder();
builder.append(String.format(Locale.US, "Report date: %s\n", new Date(System.currentTimeMillis()))); builder.append(String.format(Locale.US, "Report date: %s\n", new Date(System.currentTimeMillis())));
builder.append(String.format(Locale.US, "Android version: %s\n", Build.VERSION.RELEASE)); builder.append(String.format(Locale.US, "Android version: %s\n", Build.VERSION.RELEASE));
builder.append(String.format(Locale.US, "API version: %d\n", Build.VERSION.SDK_INT)); builder.append(String.format(Locale.US, "API version: %d\n", Build.VERSION.SDK_INT));
builder.append(String.format(Locale.US, "App version name: %s\n", getAppVersionName())); builder.append(String.format(Locale.US, "App version name: %s\n", getAppVersionName()));
builder.append(String.format(Locale.US, "App version code: %d\n", getAppVersionCode())); builder.append(String.format(Locale.US, "App version code: %d\n", getAppVersionCode()));
builder.append(String.format(Locale.US, "Configuration: %s\n", mContext.getResources().getConfiguration())); builder.append(String.format(Locale.US, "Configuration: %s\n", mContext.getResources().getConfiguration()));
builder.append(String.format(Locale.US, "Stack trace:\n%s\n", stack_trace)); builder.append(String.format(Locale.US, "Stack trace:\n%s\n", stack_trace));
return builder.toString(); return builder.toString();
} }
private CharSequence getAppName() { private CharSequence getAppName() {
final PackageManager pm = mContext.getPackageManager(); final PackageManager pm = mContext.getPackageManager();
try { try {
return pm.getApplicationLabel(pm.getApplicationInfo(mContext.getPackageName(), 0)); return pm.getApplicationLabel(pm.getApplicationInfo(mContext.getPackageName(), 0));
} catch (final NameNotFoundException e) { } catch (final NameNotFoundException e) {
return APP_NAME; return APP_NAME;
} }
} }
private int getAppVersionCode() { private int getAppVersionCode() {
final PackageManager pm = mContext.getPackageManager(); final PackageManager pm = mContext.getPackageManager();
try { try {
return pm.getPackageInfo(mContext.getPackageName(), 0).versionCode; return pm.getPackageInfo(mContext.getPackageName(), 0).versionCode;
} catch (final NameNotFoundException e) { } catch (final NameNotFoundException e) {
return 0; return 0;
} }
} }
private String getAppVersionName() { private String getAppVersionName() {
final PackageManager pm = mContext.getPackageManager(); final PackageManager pm = mContext.getPackageManager();
try { try {
return pm.getPackageInfo(mContext.getPackageName(), 0).versionName; return pm.getPackageInfo(mContext.getPackageName(), 0).versionName;
} catch (final NameNotFoundException e) { } catch (final NameNotFoundException e) {
return "unknown"; return "unknown";
} }
} }
} }
} }