code cleanup
This commit is contained in:
parent
8d662541bd
commit
e4193631da
|
@ -1,47 +0,0 @@
|
|||
/*
|
||||
* Twidere - Twitter client for Android
|
||||
*
|
||||
* Copyright (C) 2012-2015 Mariotaku Lee <mariotaku.lee@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.mariotaku.twidere.util;
|
||||
|
||||
import android.os.AsyncTask;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
* Created by mariotaku on 15/3/30.
|
||||
*/
|
||||
public class AsyncTaskUtils {
|
||||
|
||||
public static final Executor DEFAULT_EXECUTOR = Executors.newFixedThreadPool(2);
|
||||
|
||||
private AsyncTaskUtils() {
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public static <T extends AsyncTask<Parameter, ?, ?>, Parameter> T executeTask(T task, Parameter... params) {
|
||||
task.executeOnExecutor(DEFAULT_EXECUTOR, params);
|
||||
return task;
|
||||
}
|
||||
|
||||
public static boolean isTaskRunning(@Nullable AsyncTask task) {
|
||||
return task != null && task.getStatus() == AsyncTask.Status.RUNNING;
|
||||
}
|
||||
}
|
|
@ -1,66 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2010 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.mariotaku.twidere.util;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.net.Uri;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import org.mariotaku.restfu.RestFuUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class BitmapUtils {
|
||||
|
||||
private BitmapUtils() {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String getImageMimeType(ContentResolver cr, final Uri uri) {
|
||||
if (uri == null) return null;
|
||||
final BitmapFactory.Options o = new BitmapFactory.Options();
|
||||
o.inJustDecodeBounds = true;
|
||||
InputStream is = null;
|
||||
try {
|
||||
is = cr.openInputStream(uri);
|
||||
BitmapFactory.decodeStream(is, null, o);
|
||||
return o.outMimeType;
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
} finally {
|
||||
RestFuUtils.closeSilently(is);
|
||||
}
|
||||
}
|
||||
|
||||
public static String getImageMimeType(final File image) {
|
||||
if (image == null) return null;
|
||||
final BitmapFactory.Options o = new BitmapFactory.Options();
|
||||
o.inJustDecodeBounds = true;
|
||||
BitmapFactory.decodeFile(image.getPath(), o);
|
||||
return o.outMimeType;
|
||||
}
|
||||
|
||||
public static int calculateInSampleSize(final int width, final int height, final int preferredWidth,
|
||||
final int preferredHeight) {
|
||||
if (preferredHeight > height && preferredWidth > width) return 1;
|
||||
final int result = Math.round(Math.max(width, height) / (float) Math.max(preferredWidth, preferredHeight));
|
||||
return Math.max(1, result);
|
||||
}
|
||||
}
|
|
@ -539,8 +539,7 @@ class HomeActivity : BaseActivity(), OnClickListener, OnPageChangeListener, Supp
|
|||
if (mainTabs == null || updateUnreadCountTask != null && updateUnreadCountTask!!.status == AsyncTask.Status.RUNNING)
|
||||
return
|
||||
updateUnreadCountTask = UpdateUnreadCountTask(this, preferences, readStateManager, mainTabs,
|
||||
pagerAdapter.tabs.toTypedArray())
|
||||
AsyncTaskUtils.executeTask<UpdateUnreadCountTask, Any>(updateUnreadCountTask)
|
||||
pagerAdapter.tabs.toTypedArray()).apply { execute() }
|
||||
mainTabs.setDisplayBadge(preferences.getBoolean(SharedPreferenceConstants.KEY_UNREAD_COUNT, true))
|
||||
}
|
||||
|
||||
|
|
|
@ -65,7 +65,6 @@ import org.mariotaku.twidere.provider.CacheProvider
|
|||
import org.mariotaku.twidere.provider.ShareProvider
|
||||
import org.mariotaku.twidere.task.SaveFileTask
|
||||
import org.mariotaku.twidere.task.SaveMediaToGalleryTask
|
||||
import org.mariotaku.twidere.util.AsyncTaskUtils
|
||||
import org.mariotaku.twidere.util.IntentUtils
|
||||
import org.mariotaku.twidere.util.PermissionUtils
|
||||
import org.mariotaku.twidere.util.ThemeUtils
|
||||
|
@ -502,7 +501,7 @@ class MediaViewerActivity : BaseActivity(), IMediaViewerActivity, MediaSwipeClos
|
|||
Toast.makeText(activity, R.string.message_toast_error_occurred, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
AsyncTaskUtils.executeTask(task)
|
||||
task.execute()
|
||||
}
|
||||
|
||||
private fun saveToStorage() {
|
||||
|
@ -525,7 +524,7 @@ class MediaViewerActivity : BaseActivity(), IMediaViewerActivity, MediaSwipeClos
|
|||
}
|
||||
val saveDir = File(pubDir, "Twidere")
|
||||
val task = SaveMediaToGalleryTask(this, fileInfo, saveDir)
|
||||
AsyncTaskUtils.executeTask(task)
|
||||
task.execute()
|
||||
}
|
||||
|
||||
private fun MediaViewerFragment.cacheFileInfo(): SaveFileTask.FileInfo? {
|
||||
|
|
|
@ -391,8 +391,7 @@ class SignInActivity : BaseActivity(), OnClickListener, TextWatcher,
|
|||
signInTask!!.cancel(true)
|
||||
}
|
||||
|
||||
signInTask = SignInTask(this, username, password, apiConfig)
|
||||
AsyncTaskUtils.executeTask<AbstractSignInTask, Any>(signInTask)
|
||||
signInTask = SignInTask(this, username, password, apiConfig).apply { execute() }
|
||||
}
|
||||
|
||||
private fun onSignInResult(result: SignInResponse) {
|
||||
|
@ -468,8 +467,7 @@ class SignInActivity : BaseActivity(), OnClickListener, TextWatcher,
|
|||
|
||||
|
||||
private fun finishMastodonBrowserLogin(host: String, clientId: String, clientSecret: String, code: String) {
|
||||
signInTask = MastodonLoginTask(this, host, clientId, clientSecret, code)
|
||||
AsyncTaskUtils.executeTask(signInTask)
|
||||
signInTask = MastodonLoginTask(this, host, clientId, clientSecret, code).apply { execute() }
|
||||
}
|
||||
|
||||
private fun handleBrowserLoginResult(intent: Intent?) {
|
||||
|
@ -478,8 +476,7 @@ class SignInActivity : BaseActivity(), OnClickListener, TextWatcher,
|
|||
val requestToken = OAuthToken(extras.getString(EXTRA_REQUEST_TOKEN),
|
||||
extras.getString(EXTRA_REQUEST_TOKEN_SECRET))
|
||||
val verifier = intent.getStringExtra(EXTRA_OAUTH_VERIFIER)
|
||||
signInTask = BrowserSignInTask(this, apiConfig, requestToken, verifier)
|
||||
AsyncTaskUtils.executeTask(signInTask)
|
||||
signInTask = BrowserSignInTask(this, apiConfig, requestToken, verifier).apply { execute() }
|
||||
}
|
||||
|
||||
private fun setDefaultAPI() {
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
package org.mariotaku.twidere.extension
|
||||
|
||||
import android.graphics.BitmapFactory
|
||||
|
||||
fun BitmapFactory.Options.calculateInSampleSize(preferredWidth: Int, preferredHeight: Int): Int {
|
||||
if (preferredHeight > outHeight && preferredWidth > outWidth) {
|
||||
return 1
|
||||
}
|
||||
val result = Math.round(Math.max(outWidth, outHeight) / Math.max(preferredWidth, preferredHeight).toFloat())
|
||||
return Math.max(1, result)
|
||||
}
|
|
@ -61,7 +61,6 @@ import org.mariotaku.twidere.model.draft.QuoteStatusActionExtras
|
|||
import org.mariotaku.twidere.provider.TwidereDataStore.Drafts
|
||||
import org.mariotaku.twidere.service.LengthyOperationsService
|
||||
import org.mariotaku.twidere.util.Analyzer
|
||||
import org.mariotaku.twidere.util.AsyncTaskUtils
|
||||
import org.mariotaku.twidere.util.deleteDrafts
|
||||
import org.mariotaku.twidere.util.premium.ExtraFeaturesService
|
||||
import java.lang.ref.WeakReference
|
||||
|
@ -270,7 +269,7 @@ class DraftsFragment : BaseFragment(), LoaderCallbacks<Cursor?>, OnItemClickList
|
|||
when (which) {
|
||||
DialogInterface.BUTTON_POSITIVE -> {
|
||||
val args = arguments ?: return
|
||||
AsyncTaskUtils.executeTask(DeleteDraftsTask(activity, args.getLongArray(EXTRA_IDS)))
|
||||
DeleteDraftsTask(activity, args.getLongArray(EXTRA_IDS)).execute()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import android.content.ContentResolver
|
|||
import android.content.ContentValues
|
||||
import android.content.Context
|
||||
import android.database.Cursor
|
||||
import android.graphics.BitmapFactory
|
||||
import android.net.Uri
|
||||
import android.os.ParcelFileDescriptor
|
||||
import okio.ByteString
|
||||
|
@ -14,7 +15,6 @@ import org.mariotaku.twidere.TwidereConstants.QUERY_PARAM_TYPE
|
|||
import org.mariotaku.twidere.annotation.CacheFileType
|
||||
import org.mariotaku.twidere.model.CacheMetadata
|
||||
import org.mariotaku.twidere.task.SaveFileTask
|
||||
import org.mariotaku.twidere.util.BitmapUtils
|
||||
import org.mariotaku.twidere.util.JsonSerializer
|
||||
import org.mariotaku.twidere.util.dagger.GeneralComponent
|
||||
import java.io.ByteArrayInputStream
|
||||
|
@ -49,7 +49,10 @@ class CacheProvider : ContentProvider() {
|
|||
when (type) {
|
||||
CacheFileType.IMAGE -> {
|
||||
val file = fileCache.get(getCacheKey(uri)) ?: return null
|
||||
return BitmapUtils.getImageMimeType(file)
|
||||
return BitmapFactory.Options().apply {
|
||||
inJustDecodeBounds = true
|
||||
BitmapFactory.decodeFile(file.absolutePath, this)
|
||||
}.outMimeType
|
||||
}
|
||||
CacheFileType.VIDEO -> {
|
||||
return "video/mp4"
|
||||
|
|
|
@ -39,6 +39,7 @@ import org.mariotaku.twidere.TwidereConstants.*
|
|||
import org.mariotaku.twidere.alias.MastodonStatusUpdate
|
||||
import org.mariotaku.twidere.annotation.AccountType
|
||||
import org.mariotaku.twidere.app.TwidereApplication
|
||||
import org.mariotaku.twidere.extension.calculateInSampleSize
|
||||
import org.mariotaku.twidere.extension.model.api.mastodon.toParcelable
|
||||
import org.mariotaku.twidere.extension.model.api.toParcelable
|
||||
import org.mariotaku.twidere.extension.model.applyUpdateStatus
|
||||
|
@ -908,8 +909,7 @@ class UpdateStatusTask(
|
|||
}
|
||||
|
||||
if (imageLimit == null || imageLimit.checkGeomentry(o.outWidth, o.outHeight)) return null
|
||||
o.inSampleSize = BitmapUtils.calculateInSampleSize(o.outWidth, o.outHeight,
|
||||
imageLimit.maxWidth, imageLimit.maxHeight)
|
||||
o.inSampleSize = o.calculateInSampleSize(imageLimit.maxWidth, imageLimit.maxHeight)
|
||||
o.inJustDecodeBounds = false
|
||||
// Do actual image decoding
|
||||
val bitmap = context.contentResolver.openInputStream(mediaUri).use {
|
||||
|
|
Loading…
Reference in New Issue