code cleanup

This commit is contained in:
Mariotaku Lee 2016-12-18 10:04:02 +08:00
parent 7af26a34b9
commit 8398953ec8
13 changed files with 57 additions and 41 deletions

View File

@ -23,6 +23,7 @@ public class ErrorInfoStore {
public static final int CODE_NO_DM_PERMISSION = 1;
public static final int CODE_NO_ACCESS_FOR_CREDENTIALS = 2;
public static final int CODE_NETWORK_ERROR = 3;
public static final int CODE_TIMESTAMP_ERROR = 4;
private final SharedPreferences mPreferences;
@ -47,20 +48,20 @@ public class ErrorInfoStore {
}
}
public void put(String key, int code) {
public void set(String key, int code) {
mPreferences.edit().putInt(key, code).apply();
}
public void put(String key, String extraId, int code) {
put(key + "_" + extraId, code);
public void set(String key, String extraId, int code) {
set(key + "_" + extraId, code);
}
public void put(String key, UserKey extraId, int code) {
public void set(String key, UserKey extraId, int code) {
final String host = extraId.getHost();
if (host == null) {
put(key, extraId.getId(), code);
set(key, extraId.getId(), code);
} else {
put(key + "_" + extraId.getId() + "_" + host, code);
set(key + "_" + extraId.getId() + "_" + host, code);
}
}
@ -79,6 +80,10 @@ public class ErrorInfoStore {
return new DisplayErrorInfo(code, R.drawable.ic_info_error_generic,
context.getString(R.string.network_error));
}
case CODE_TIMESTAMP_ERROR: {
return new DisplayErrorInfo(code, R.drawable.ic_info_error_generic,
context.getString(R.string.error_info_oauth_timestamp_error));
}
}
return null;
}

View File

@ -1027,8 +1027,10 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener
}
}
mentions.filterNot { it.equals(status.user_screen_name, ignoreCase = true)
|| it.equals(accountUser.screen_name, ignoreCase = true) }
mentions.filterNot {
it.equals(status.user_screen_name, ignoreCase = true)
|| it.equals(accountUser.screen_name, ignoreCase = true)
}
.forEach { editText.append("@$it ") }
val selectionEnd = editText.length()
editText.setSelection(selectionStart, selectionEnd)
@ -1776,7 +1778,10 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener
override fun onClick(v: View) {
when (v.id) {
R.id.remove -> {
adapter?.remove(layoutPosition)
val adapter = this.adapter ?: return
if (layoutPosition >= 0 && layoutPosition < adapter.itemCount) {
adapter.remove(layoutPosition)
}
}
R.id.edit -> {
itemView.parent.showContextMenuForChild(itemView)
@ -1796,7 +1801,9 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener
(activity as ComposeActivity).setMediaAltText(arguments.getInt(EXTRA_POSITION),
ParseUtils.parseString(editText.text))
}
builder.setNeutralButton(R.string.clear) { dialogInterface, i -> (activity as ComposeActivity).setMediaAltText(arguments.getInt(EXTRA_POSITION), null) }
builder.setNeutralButton(R.string.clear) { dialogInterface, i ->
(activity as ComposeActivity).setMediaAltText(arguments.getInt(EXTRA_POSITION), null)
}
val dialog = builder.create()
dialog.setOnShowListener { dialog ->
val materialDialog = dialog as Dialog

View File

@ -61,7 +61,7 @@ abstract class CursorActivitiesFragment : AbsActivitiesFragment() {
showContent()
} else if (accountKeys.isNotEmpty()) {
val errorInfo = ErrorInfoStore.getErrorInfo(context,
errorInfoStore.get(errorInfoKey, accountKeys[0]))
errorInfoStore[errorInfoKey, accountKeys[0]])
if (errorInfo != null) {
showEmpty(errorInfo.icon, errorInfo.message)
} else {

View File

@ -109,7 +109,7 @@ abstract class CursorStatusesFragment : AbsStatusesFragment() {
showContent()
} else if (accountKeys.isNotEmpty()) {
val errorInfo = ErrorInfoStore.getErrorInfo(context,
errorInfoStore.get(errorInfoKey, accountKeys[0]))
errorInfoStore[errorInfoKey, accountKeys[0]])
if (errorInfo != null) {
showEmpty(errorInfo.icon, errorInfo.message)
} else {

View File

@ -167,7 +167,7 @@ class DirectMessagesFragment : AbsContentListRecyclerViewFragment<MessageEntries
if (accountIds.isNotEmpty()) {
val errorInfo = ErrorInfoStore.getErrorInfo(context,
errorInfoStore.get(ErrorInfoStore.KEY_DIRECT_MESSAGES, accountIds[0]))
errorInfoStore[ErrorInfoStore.KEY_DIRECT_MESSAGES, accountIds.first()])
if (isEmpty && errorInfo != null) {
showEmpty(errorInfo.icon, errorInfo.message)
} else {

View File

@ -411,7 +411,7 @@ class StatusFragment : BaseSupportFragment(), LoaderCallbacks<SingleResponse<Par
if (status != null) {
val readPosition = saveReadPosition()
val dataExtra = data.extras
val details: AccountDetails = dataExtra.getParcelable(EXTRA_ACCOUNT)
val details: AccountDetails? = dataExtra.getParcelable(EXTRA_ACCOUNT)
if (adapter.setStatus(status, details)) {
val args = arguments
if (args.containsKey(EXTRA_STATUS)) {
@ -429,7 +429,11 @@ class StatusFragment : BaseSupportFragment(), LoaderCallbacks<SingleResponse<Par
val event = TweetEvent.create(activity, status, TimelineType.OTHER)
event.action = TweetEvent.Action.OPEN
if (details != null) {
event.isHasTranslateFeature = Utils.isOfficialCredentials(context, details)
} else {
event.isHasTranslateFeature = false
}
statusEvent = event
} else if (readPosition != null) {
restoreReadPosition(readPosition)
@ -1816,7 +1820,7 @@ class StatusFragment : BaseSupportFragment(), LoaderCallbacks<SingleResponse<Par
updateItemDecoration()
}
fun setStatus(status: ParcelableStatus, account: AccountDetails): Boolean {
fun setStatus(status: ParcelableStatus, account: AccountDetails?): Boolean {
val old = this.status
this.status = status
statusAccount = account

View File

@ -23,6 +23,7 @@ import android.accounts.AccountManager
import android.content.Context
import android.os.Bundle
import android.support.v4.content.AsyncTaskLoader
import org.mariotaku.ktextension.set
import org.mariotaku.microblog.library.MicroBlogException
import org.mariotaku.microblog.library.twitter.model.ErrorInfo
import org.mariotaku.twidere.constant.IntentConstants
@ -57,24 +58,24 @@ class ParcelableStatusLoader(
}
override fun loadInBackground(): SingleResponse<ParcelableStatus> {
if (accountKey == null || statusId == null) return SingleResponse.getInstance<ParcelableStatus>()
if (accountKey == null || statusId == null) {
return SingleResponse(exception = IllegalArgumentException())
}
val details = AccountUtils.getAccountDetails(AccountManager.get(context), accountKey, true)
if (!omitIntentExtra && extras != null) {
val cache = extras.getParcelable<ParcelableStatus>(IntentConstants.EXTRA_STATUS)
val cache: ParcelableStatus? = extras.getParcelable(IntentConstants.EXTRA_STATUS)
if (cache != null) {
val response = SingleResponse.getInstance(cache)
val extras = response.extras
extras.putParcelable(EXTRA_ACCOUNT, details)
val response = SingleResponse(cache)
response.extras[EXTRA_ACCOUNT] = details
return response
}
}
if (details == null) return SingleResponse(exception = MicroBlogException("No account"))
try {
if (details == null) return SingleResponse.getInstance<ParcelableStatus>()
val status = findStatus(context, accountKey, statusId)
ParcelableStatusUtils.updateExtraInformation(status, details, userColorNameManager)
val response = SingleResponse.getInstance(status)
val extras = response.extras
extras.putParcelable(EXTRA_ACCOUNT, details)
val response = SingleResponse(status)
response.extras[EXTRA_ACCOUNT] = details
return response
} catch (e: MicroBlogException) {
if (e.errorCode == ErrorInfo.STATUS_NOT_FOUND) {
@ -84,7 +85,7 @@ class ParcelableStatusLoader(
statusId, null)
DataStoreUtils.deleteActivityStatus(cr, accountKey, statusId, null)
}
return SingleResponse.getInstance<ParcelableStatus>(e)
return SingleResponse(exception = e)
}
}

View File

@ -90,21 +90,21 @@ class RefreshService : Service() {
when (refreshType) {
AutoRefreshType.HOME_TIMELINE -> {
val task = GetHomeTimelineTask(context)
task.params = RefreshService.AutoRefreshTaskParam(context, AccountPreferences::isAutoRefreshHomeTimelineEnabled) { accountKeys ->
task.params = AutoRefreshTaskParam(context, AccountPreferences::isAutoRefreshHomeTimelineEnabled) { accountKeys ->
DataStoreUtils.getNewestStatusIds(context, Statuses.CONTENT_URI, accountKeys)
}
return task
}
AutoRefreshType.INTERACTIONS_TIMELINE -> {
val task = GetActivitiesAboutMeTask(context)
task.params = RefreshService.AutoRefreshTaskParam(context, AccountPreferences::isAutoRefreshMentionsEnabled) { accountKeys ->
task.params = AutoRefreshTaskParam(context, AccountPreferences::isAutoRefreshMentionsEnabled) { accountKeys ->
DataStoreUtils.getNewestActivityMaxPositions(context, Activities.AboutMe.CONTENT_URI, accountKeys)
}
return task
}
AutoRefreshType.DIRECT_MESSAGES -> {
val task = GetReceivedDirectMessagesTask(context)
task.params = RefreshService.AutoRefreshTaskParam(context, AccountPreferences::isAutoRefreshDirectMessagesEnabled) { accountKeys ->
task.params = AutoRefreshTaskParam(context, AccountPreferences::isAutoRefreshDirectMessagesEnabled) { accountKeys ->
DataStoreUtils.getNewestMessageIds(context, DirectMessages.Inbox.CONTENT_URI, accountKeys)
}
return task

View File

@ -86,11 +86,9 @@ abstract class GetDirectMessagesTask(
errorInfoStore.remove(ErrorInfoStore.KEY_DIRECT_MESSAGES, accountKey)
} catch (e: MicroBlogException) {
if (e.errorCode == ErrorInfo.NO_DIRECT_MESSAGE_PERMISSION) {
errorInfoStore.put(ErrorInfoStore.KEY_DIRECT_MESSAGES, accountKey,
ErrorInfoStore.CODE_NO_DM_PERMISSION)
errorInfoStore[ErrorInfoStore.KEY_DIRECT_MESSAGES, accountKey] = ErrorInfoStore.CODE_NO_DM_PERMISSION
} else if (e.isCausedByNetworkIssue) {
errorInfoStore.put(ErrorInfoStore.KEY_DIRECT_MESSAGES, accountKey,
ErrorInfoStore.CODE_NETWORK_ERROR)
errorInfoStore[ErrorInfoStore.KEY_DIRECT_MESSAGES, accountKey] = ErrorInfoStore.CODE_NETWORK_ERROR
}
if (BuildConfig.DEBUG) {
Log.w(TwidereConstants.LOGTAG, e)

View File

@ -107,11 +107,9 @@ abstract class GetActivitiesTask(
Log.w(LOGTAG, e)
}
if (e.errorCode == 220) {
errorInfoStore.put(errorInfoKey, accountKey,
ErrorInfoStore.CODE_NO_ACCESS_FOR_CREDENTIALS)
errorInfoStore[errorInfoKey, accountKey] = ErrorInfoStore.CODE_NO_ACCESS_FOR_CREDENTIALS
} else if (e.isCausedByNetworkIssue) {
errorInfoStore.put(errorInfoKey, accountKey,
ErrorInfoStore.CODE_NETWORK_ERROR)
errorInfoStore[errorInfoKey, accountKey] = ErrorInfoStore.CODE_NETWORK_ERROR
}
}

View File

@ -143,8 +143,9 @@ abstract class GetStatusesTask(
Log.w(LOGTAG, e)
}
if (e.isCausedByNetworkIssue) {
errorInfoStore.put(errorInfoKey, accountKey.id,
ErrorInfoStore.CODE_NETWORK_ERROR)
errorInfoStore[errorInfoKey, accountKey.id] = ErrorInfoStore.CODE_NETWORK_ERROR
} else if (e.statusCode == 401) {
// Unauthorized
}
result.add(TwitterWrapper.StatusListResponse(accountKey, e))
}

View File

@ -4,6 +4,7 @@ import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.SystemClock
import android.support.v4.util.ArrayMap
import org.mariotaku.kpreferences.KPreferences
import org.mariotaku.twidere.annotation.AutoRefreshType
@ -46,8 +47,8 @@ class LegacyAutoRefreshController(
val pendingIntent = pendingIntents[type] ?: return
val interval = TimeUnit.MINUTES.toMillis(kPreferences[refreshIntervalKey])
if (interval > 0) {
val triggerAt = System.currentTimeMillis() + interval
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, triggerAt, interval, pendingIntent)
val triggerAt = SystemClock.elapsedRealtime() + interval
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAt, interval, pendingIntent)
}
}

View File

@ -818,4 +818,5 @@
<string name="preference_randomize_account_name_summary">Disallow other apps to get your name by reading accounts, improves privacy.</string>
<string name="preference_randomize_account_rename_accounts_confirm">Rename existing accounts?</string>
<string name="message_auto_refresh_confirm">Enable auto refresh to get new tweets automatically?</string>
<string name="error_info_oauth_timestamp_error">Please check your system date &amp; time settings.</string>
</resources>