code cleanup

This commit is contained in:
Mariotaku Lee 2018-05-14 15:24:01 +08:00
parent ec49301585
commit 42361c9fbe
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
24 changed files with 129 additions and 111 deletions

View File

@ -1 +1 @@
e42684a358dfcd05b5ad3d2d544f613ba7259256 dd0844dbbe382a92fb135be9979788e5f2f6d312

View File

@ -1,6 +1,7 @@
package org.mariotaku.twidere.model.tab; package org.mariotaku.twidere.model.tab;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
@ -10,6 +11,7 @@ import org.mariotaku.kpreferences.SharedPreferencesExtensionsKt;
import org.mariotaku.twidere.R; import org.mariotaku.twidere.R;
import org.mariotaku.twidere.constant.PreferenceKeysKt; import org.mariotaku.twidere.constant.PreferenceKeysKt;
import org.mariotaku.twidere.dagger.DependencyHolder; import org.mariotaku.twidere.dagger.DependencyHolder;
import org.mariotaku.twidere.singleton.PreferencesSingleton;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -203,7 +205,8 @@ public abstract class DrawableHolder {
@Override @Override
public Drawable createDrawable(Context context) { public Drawable createDrawable(Context context) {
final DependencyHolder holder = DependencyHolder.Companion.get(context); final DependencyHolder holder = DependencyHolder.Companion.get(context);
if (SharedPreferencesExtensionsKt.get(holder.preferences, PreferenceKeysKt.getIWantMyStarsBackKey())) { final SharedPreferences preferences = PreferencesSingleton.INSTANCE.get(context);
if (SharedPreferencesExtensionsKt.get(preferences, PreferenceKeysKt.getIWantMyStarsBackKey())) {
return ContextCompat.getDrawable(context, R.drawable.ic_action_star); return ContextCompat.getDrawable(context, R.drawable.ic_action_star);
} }
return ContextCompat.getDrawable(context, R.drawable.ic_action_heart); return ContextCompat.getDrawable(context, R.drawable.ic_action_heart);

View File

@ -119,11 +119,11 @@ open class BaseActivity : ChameleonActivity(), IBaseActivity<BaseActivity>, IThe
protected val isDialogTheme: Boolean protected val isDialogTheme: Boolean
get() = ThemeUtils.getBooleanFromAttribute(this, R.attr.isDialogTheme) get() = ThemeUtils.getBooleanFromAttribute(this, R.attr.isDialogTheme)
override final val currentThemeBackgroundAlpha by lazy { final override val currentThemeBackgroundAlpha by lazy {
themeBackgroundAlpha themeBackgroundAlpha
} }
override final val currentThemeBackgroundOption by lazy { final override val currentThemeBackgroundOption by lazy {
themeBackgroundOption themeBackgroundOption
} }

View File

@ -355,7 +355,6 @@ class MediaViewerActivity : BaseActivity(), IMediaViewerActivity, MediaSwipeClos
when (media.type) { when (media.type) {
ParcelableMedia.Type.IMAGE -> { ParcelableMedia.Type.IMAGE -> {
val mediaUrl = media.media_url val mediaUrl = media.media_url
?: return Fragment.instantiate(this, ExternalBrowserPageFragment::class.java.name, args) as MediaViewerFragment
args.putParcelable(EXTRA_MEDIA_URI, Uri.parse(mediaUrl)) args.putParcelable(EXTRA_MEDIA_URI, Uri.parse(mediaUrl))
if (mediaUrl.endsWith(".gif")) { if (mediaUrl.endsWith(".gif")) {
return Fragment.instantiate(this, GifPageFragment::class.java.name, args) as MediaViewerFragment return Fragment.instantiate(this, GifPageFragment::class.java.name, args) as MediaViewerFragment

View File

@ -12,9 +12,9 @@ import org.mariotaku.twidere.TwidereConstants.*
import org.mariotaku.twidere.activity.content.FavoriteConfirmDialogActivity import org.mariotaku.twidere.activity.content.FavoriteConfirmDialogActivity
import org.mariotaku.twidere.activity.content.RetweetQuoteDialogActivity import org.mariotaku.twidere.activity.content.RetweetQuoteDialogActivity
import org.mariotaku.twidere.app.TwidereApplication import org.mariotaku.twidere.app.TwidereApplication
import org.mariotaku.twidere.dagger.DependencyHolder
import org.mariotaku.twidere.model.UserKey import org.mariotaku.twidere.model.UserKey
import org.mariotaku.twidere.model.theme.UserTheme import org.mariotaku.twidere.model.theme.UserTheme
import org.mariotaku.twidere.singleton.PreferencesSingleton
import org.mariotaku.twidere.util.Analyzer import org.mariotaku.twidere.util.Analyzer
import org.mariotaku.twidere.util.IntentUtils import org.mariotaku.twidere.util.IntentUtils
import java.util.* import java.util.*
@ -22,7 +22,7 @@ import java.util.*
class WebLinkHandlerActivity : Activity() { class WebLinkHandlerActivity : Activity() {
private val userTheme: Chameleon.Theme by lazy { private val userTheme: Chameleon.Theme by lazy {
val preferences = DependencyHolder.get(this).preferences val preferences = PreferencesSingleton.get(this)
return@lazy UserTheme.get(this, preferences) return@lazy UserTheme.get(this, preferences)
} }
@ -315,7 +315,7 @@ class WebLinkHandlerActivity : Activity() {
if (pathSegments.size < 2) return Pair(null, false) if (pathSegments.size < 2) return Pair(null, false)
when (pathSegments[1]) { when (pathSegments[1]) {
"moments" -> { "moments" -> {
val preferences = DependencyHolder.get(this).preferences val preferences = PreferencesSingleton.get(this)
val (intent, _) = IntentUtils.browse(this, preferences, userTheme, uri, true) val (intent, _) = IntentUtils.browse(this, preferences, userTheme, uri, true)
return Pair(intent, true) return Pair(intent, true)
} }
@ -335,7 +335,7 @@ class WebLinkHandlerActivity : Activity() {
} }
"redirect" -> { "redirect" -> {
val url = uri.getQueryParameter("url")?.let(Uri::parse) ?: return Pair(null, false) val url = uri.getQueryParameter("url")?.let(Uri::parse) ?: return Pair(null, false)
val preferences = DependencyHolder.get(this).preferences val preferences = PreferencesSingleton.get(this)
val (intent, _) = IntentUtils.browse(this, preferences, userTheme, url, false) val (intent, _) = IntentUtils.browse(this, preferences, userTheme, url, false)
return Pair(intent, true) return Pair(intent, true)
} }

View File

@ -20,8 +20,11 @@
package org.mariotaku.twidere.activity.feedback package org.mariotaku.twidere.activity.feedback
import android.os.Bundle import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import org.mariotaku.twidere.R import org.mariotaku.twidere.R
import org.mariotaku.twidere.activity.BaseActivity import org.mariotaku.twidere.activity.BaseActivity
import org.mariotaku.twidere.fragment.BaseDialogFragment
class FeedbackActivity : BaseActivity() { class FeedbackActivity : BaseActivity() {
@ -30,4 +33,24 @@ class FeedbackActivity : BaseActivity() {
setContentView(R.layout.activity_feedback) setContentView(R.layout.activity_feedback)
} }
override fun onCreateOptionsMenu(menu: Menu): Boolean {
super.onCreateOptionsMenu(menu)
menuInflater.inflate(R.menu.menu_feedback, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.send -> {
return true
}
else -> return super.onOptionsItemSelected(item)
}
}
class SendMethodDialogFragment: BaseDialogFragment() {
}
} }

View File

@ -20,26 +20,20 @@
package org.mariotaku.twidere.dagger package org.mariotaku.twidere.dagger
import android.content.Context import android.content.Context
import android.content.SharedPreferences
import okhttp3.Cache
import okhttp3.ConnectionPool
import okhttp3.Dns
import org.mariotaku.restfu.http.RestHttpClient import org.mariotaku.restfu.http.RestHttpClient
import org.mariotaku.twidere.dagger.component.GeneralComponent import org.mariotaku.twidere.dagger.component.GeneralComponent
import org.mariotaku.twidere.model.DefaultFeatures
import org.mariotaku.twidere.util.ActivityTracker import org.mariotaku.twidere.util.ActivityTracker
import org.mariotaku.twidere.util.ExternalThemeManager
import org.mariotaku.twidere.util.NotificationManagerWrapper import org.mariotaku.twidere.util.NotificationManagerWrapper
import org.mariotaku.twidere.util.ReadStateManager import org.mariotaku.twidere.util.ReadStateManager
import org.mariotaku.twidere.util.lang.ApplicationContextSingletonHolder
import org.mariotaku.twidere.util.media.MediaPreloader import org.mariotaku.twidere.util.media.MediaPreloader
import org.mariotaku.twidere.util.media.ThumborWrapper import org.mariotaku.twidere.util.media.ThumborWrapper
import org.mariotaku.twidere.util.premium.ExtraFeaturesService
import javax.inject.Inject import javax.inject.Inject
/** /**
* Created by mariotaku on 15/12/31. * Created by mariotaku on 15/12/31.
*/ */
class DependencyHolder internal constructor(context: Context) { class DependencyHolder internal constructor(val context: Context) {
@Inject @Inject
lateinit var readStateManager: ReadStateManager lateinit var readStateManager: ReadStateManager
internal set internal set
@ -47,27 +41,9 @@ class DependencyHolder internal constructor(context: Context) {
lateinit var restHttpClient: RestHttpClient lateinit var restHttpClient: RestHttpClient
internal set internal set
@Inject @Inject
lateinit var externalThemeManager: ExternalThemeManager
internal set
@Inject
lateinit var activityTracker: ActivityTracker lateinit var activityTracker: ActivityTracker
internal set internal set
@Inject @Inject
lateinit var dns: Dns
internal set
@Inject
lateinit var preferences: SharedPreferences
internal set
@Inject
lateinit var connectionPool: ConnectionPool
internal set
@Inject
lateinit var cache: Cache
internal set
@Inject
lateinit var defaultFeatures: DefaultFeatures
internal set
@Inject
lateinit var mediaPreloader: MediaPreloader lateinit var mediaPreloader: MediaPreloader
internal set internal set
@Inject @Inject
@ -75,9 +51,6 @@ class DependencyHolder internal constructor(context: Context) {
internal set internal set
@Inject @Inject
lateinit var extraFeaturesService: ExtraFeaturesService
internal set
@Inject
lateinit var notificationManager: NotificationManagerWrapper lateinit var notificationManager: NotificationManagerWrapper
internal set internal set
@ -85,14 +58,5 @@ class DependencyHolder internal constructor(context: Context) {
GeneralComponent.get(context).inject(this) GeneralComponent.get(context).inject(this)
} }
companion object { companion object :ApplicationContextSingletonHolder<DependencyHolder>(::DependencyHolder)
private var sInstance: DependencyHolder? = null
fun get(context: Context): DependencyHolder {
if (sInstance != null) return sInstance!!
sInstance = DependencyHolder(context)
return sInstance!!
}
}
} }

View File

@ -36,17 +36,16 @@ import dagger.Module
import dagger.Provides import dagger.Provides
import okhttp3.Cache import okhttp3.Cache
import okhttp3.ConnectionPool import okhttp3.ConnectionPool
import okhttp3.Dns
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import org.mariotaku.mediaviewer.library.FileCache import org.mariotaku.mediaviewer.library.FileCache
import org.mariotaku.mediaviewer.library.MediaDownloader import org.mariotaku.mediaviewer.library.MediaDownloader
import org.mariotaku.restfu.http.RestHttpClient import org.mariotaku.restfu.http.RestHttpClient
import org.mariotaku.restfu.okhttp3.OkHttpRestClient import org.mariotaku.restfu.okhttp3.OkHttpRestClient
import org.mariotaku.twidere.Constants.* import org.mariotaku.twidere.Constants.*
import org.mariotaku.twidere.constant.SharedPreferenceConstants.KEY_CACHE_SIZE_LIMIT
import org.mariotaku.twidere.extension.model.load import org.mariotaku.twidere.extension.model.load
import org.mariotaku.twidere.model.DefaultFeatures import org.mariotaku.twidere.model.DefaultFeatures
import org.mariotaku.twidere.singleton.BusSingleton import org.mariotaku.twidere.singleton.BusSingleton
import org.mariotaku.twidere.singleton.CacheSingleton
import org.mariotaku.twidere.taskcontroller.refresh.RefreshTaskController import org.mariotaku.twidere.taskcontroller.refresh.RefreshTaskController
import org.mariotaku.twidere.taskcontroller.sync.JobSchedulerSyncController import org.mariotaku.twidere.taskcontroller.sync.JobSchedulerSyncController
import org.mariotaku.twidere.taskcontroller.sync.LegacySyncController import org.mariotaku.twidere.taskcontroller.sync.LegacySyncController
@ -131,8 +130,9 @@ class ApplicationModule(private val application: Application) {
@Provides @Provides
@Singleton @Singleton
fun restHttpClient(prefs: SharedPreferences, dns: Dns, connectionPool: ConnectionPool, fun restHttpClient(prefs: SharedPreferences): RestHttpClient {
cache: Cache): RestHttpClient { val dns = TwidereDns.get(application)
val cache = CacheSingleton.get(application)
val conf = HttpClientFactory.HttpClientConfiguration(prefs) val conf = HttpClientFactory.HttpClientConfiguration(prefs)
val client = HttpClientFactory.createRestHttpClient(conf, dns, cache) val client = HttpClientFactory.createRestHttpClient(conf, dns, cache)
PreferenceChangeNotifier.get(application).register(KEY_ENABLE_PROXY, KEY_PROXY_ADDRESS, KEY_PROXY_TYPE, PreferenceChangeNotifier.get(application).register(KEY_ENABLE_PROXY, KEY_PROXY_ADDRESS, KEY_PROXY_TYPE,
@ -191,16 +191,6 @@ class ApplicationModule(private val application: Application) {
return preloader return preloader
} }
@Provides
@Singleton
fun dns(preferences: SharedPreferences): Dns {
val dns = TwidereDns(application, preferences)
PreferenceChangeNotifier.get(application).register(KEY_DNS_SERVER, KEY_TCP_DNS_QUERY, KEY_BUILTIN_DNS_RESOLVER) {
dns.reloadDnsSettings()
}
return dns
}
@Provides @Provides
@Singleton @Singleton
fun mediaDownloader(client: RestHttpClient, thumbor: ThumborWrapper): MediaDownloader { fun mediaDownloader(client: RestHttpClient, thumbor: ThumborWrapper): MediaDownloader {
@ -292,8 +282,9 @@ class ApplicationModule(private val application: Application) {
@Provides @Provides
@Singleton @Singleton
fun okHttpClient(preferences: SharedPreferences, dns: Dns, connectionPool: ConnectionPool, fun okHttpClient(preferences: SharedPreferences): OkHttpClient {
cache: Cache): OkHttpClient { val dns = TwidereDns.get(application)
val cache = CacheSingleton.get(application)
val conf = HttpClientFactory.HttpClientConfiguration(preferences) val conf = HttpClientFactory.HttpClientConfiguration(preferences)
val builder = OkHttpClient.Builder() val builder = OkHttpClient.Builder()
HttpClientFactory.initOkHttpClient(conf, builder, dns, cache) HttpClientFactory.initOkHttpClient(conf, builder, dns, cache)
@ -302,7 +293,9 @@ class ApplicationModule(private val application: Application) {
@Provides @Provides
@Singleton @Singleton
fun dataSourceFactory(preferences: SharedPreferences, dns: Dns, cache: Cache): DataSource.Factory { fun dataSourceFactory(preferences: SharedPreferences): DataSource.Factory {
val dns = TwidereDns.get(application)
val cache = CacheSingleton.get(application)
val conf = HttpClientFactory.HttpClientConfiguration(preferences) val conf = HttpClientFactory.HttpClientConfiguration(preferences)
val builder = OkHttpClient.Builder() val builder = OkHttpClient.Builder()
HttpClientFactory.initOkHttpClient(conf, builder, dns, cache) HttpClientFactory.initOkHttpClient(conf, builder, dns, cache)
@ -312,10 +305,8 @@ class ApplicationModule(private val application: Application) {
@Provides @Provides
@Singleton @Singleton
fun cache(preferences: SharedPreferences): Cache { fun cache(): Cache {
val cacheSizeMB = preferences.getInt(KEY_CACHE_SIZE_LIMIT, 300).coerceIn(100..500) return CacheSingleton.get(application)
// Convert to bytes
return Cache(getCacheDir("network", cacheSizeMB * 1048576L), cacheSizeMB * 1048576L)
} }
@Provides @Provides

View File

@ -23,6 +23,8 @@ import android.content.Context
import android.content.SharedPreferences import android.content.SharedPreferences
import android.os.Build import android.os.Build
import org.mariotaku.twidere.TwidereConstants import org.mariotaku.twidere.TwidereConstants
import org.mariotaku.twidere.util.Utils
import java.io.File
import java.util.* import java.util.*
val Context.defaultSharedPreferences: SharedPreferences val Context.defaultSharedPreferences: SharedPreferences
@ -36,3 +38,8 @@ fun Context.overriding(withLocale: Locale): Context {
this this
} }
} }
fun Context.getCacheDir(dirName: String, sizeInBytes: Long): File {
return Utils.getExternalCacheDir(this, dirName, sizeInBytes)
?: Utils.getInternalCacheDir(this, dirName)
}

View File

@ -4,13 +4,9 @@ import android.content.Context
import android.net.Uri import android.net.Uri
import android.text.TextUtils import android.text.TextUtils
import org.mariotaku.microblog.library.* import org.mariotaku.microblog.library.*
import org.mariotaku.microblog.library.FanfouStream
import org.mariotaku.microblog.library.Mastodon
import org.mariotaku.microblog.library.MastodonOAuth2
import org.mariotaku.microblog.library.MastodonStreaming
import org.mariotaku.microblog.library.twitter.*
import org.mariotaku.microblog.library.auth.BasicAuthorization import org.mariotaku.microblog.library.auth.BasicAuthorization
import org.mariotaku.microblog.library.auth.EmptyAuthorization import org.mariotaku.microblog.library.auth.EmptyAuthorization
import org.mariotaku.microblog.library.twitter.TwitterWeb
import org.mariotaku.restfu.RestAPIFactory import org.mariotaku.restfu.RestAPIFactory
import org.mariotaku.restfu.RestRequest import org.mariotaku.restfu.RestRequest
import org.mariotaku.restfu.http.Authorization import org.mariotaku.restfu.http.Authorization
@ -24,6 +20,8 @@ import org.mariotaku.twidere.Constants.DEFAULT_TWITTER_API_URL_FORMAT
import org.mariotaku.twidere.annotation.AccountType import org.mariotaku.twidere.annotation.AccountType
import org.mariotaku.twidere.dagger.DependencyHolder import org.mariotaku.twidere.dagger.DependencyHolder
import org.mariotaku.twidere.model.account.cred.* import org.mariotaku.twidere.model.account.cred.*
import org.mariotaku.twidere.singleton.CacheSingleton
import org.mariotaku.twidere.singleton.PreferencesSingleton
import org.mariotaku.twidere.util.HttpClientFactory import org.mariotaku.twidere.util.HttpClientFactory
import org.mariotaku.twidere.util.InternalTwitterContentUtils import org.mariotaku.twidere.util.InternalTwitterContentUtils
import org.mariotaku.twidere.util.MicroBlogAPIFactory import org.mariotaku.twidere.util.MicroBlogAPIFactory
@ -31,6 +29,7 @@ import org.mariotaku.twidere.util.MicroBlogAPIFactory.sFanfouConstantPool
import org.mariotaku.twidere.util.MicroBlogAPIFactory.sTwitterConstantPool import org.mariotaku.twidere.util.MicroBlogAPIFactory.sTwitterConstantPool
import org.mariotaku.twidere.util.api.* import org.mariotaku.twidere.util.api.*
import org.mariotaku.twidere.util.media.TwidereMediaDownloader import org.mariotaku.twidere.util.media.TwidereMediaDownloader
import org.mariotaku.twidere.util.net.TwidereDns
fun Credentials.getAuthorization(cls: Class<*>?): Authorization { fun Credentials.getAuthorization(cls: Class<*>?): Authorization {
if (cls != null) { if (cls != null) {
@ -152,21 +151,23 @@ fun <T> newMicroBlogInstance(context: Context, endpoint: Endpoint, auth: Authori
var extraRequestParams: Map<String, String>? = null var extraRequestParams: Map<String, String>? = null
when (cls) { when (cls) {
TwitterUpload::class.java -> { TwitterUpload::class.java -> {
val conf = HttpClientFactory.HttpClientConfiguration(holder.preferences) val conf = HttpClientFactory.HttpClientConfiguration(PreferencesSingleton.get(context))
// Use longer timeout for uploading // Use longer timeout for uploading
conf.readTimeoutSecs = 30 conf.readTimeoutSecs = 30
conf.writeTimeoutSecs = 30 conf.writeTimeoutSecs = 30
conf.connectionTimeoutSecs = 60 conf.connectionTimeoutSecs = 60
val uploadHttpClient = HttpClientFactory.createRestHttpClient(conf, holder.dns, val dns = TwidereDns.get(context)
holder.cache) val cache = CacheSingleton.get(context)
val uploadHttpClient = HttpClientFactory.createRestHttpClient(conf, dns, cache)
factory.setHttpClient(uploadHttpClient) factory.setHttpClient(uploadHttpClient)
} }
TwitterUserStream::class.java, FanfouStream::class.java, MastodonStreaming::class.java -> { TwitterUserStream::class.java, FanfouStream::class.java, MastodonStreaming::class.java -> {
val conf = HttpClientFactory.HttpClientConfiguration(holder.preferences) val conf = HttpClientFactory.HttpClientConfiguration(PreferencesSingleton.get(context))
// Use longer read timeout for streaming // Use longer read timeout for streaming
conf.readTimeoutSecs = 300 conf.readTimeoutSecs = 300
val streamHttpClient = HttpClientFactory.createRestHttpClient(conf, holder.dns, val dns = TwidereDns.get(context)
holder.cache) val cache = CacheSingleton.get(context)
val streamHttpClient = HttpClientFactory.createRestHttpClient(conf, dns, cache)
factory.setHttpClient(streamHttpClient) factory.setHttpClient(streamHttpClient)
} }
else -> { else -> {

View File

@ -20,14 +20,11 @@
package org.mariotaku.twidere.fragment package org.mariotaku.twidere.fragment
import android.content.Context import android.content.Context
import android.content.SharedPreferences
import android.support.v4.app.DialogFragment import android.support.v4.app.DialogFragment
import nl.komponents.kovenant.Promise import nl.komponents.kovenant.Promise
import okhttp3.Dns
import org.mariotaku.restfu.http.RestHttpClient import org.mariotaku.restfu.http.RestHttpClient
import org.mariotaku.twidere.dagger.component.GeneralComponent import org.mariotaku.twidere.dagger.component.GeneralComponent
import org.mariotaku.twidere.fragment.iface.IBaseFragment import org.mariotaku.twidere.fragment.iface.IBaseFragment
import org.mariotaku.twidere.util.DebugModeUtils
import org.mariotaku.twidere.util.KeyboardShortcutsHandler import org.mariotaku.twidere.util.KeyboardShortcutsHandler
import org.mariotaku.twidere.util.premium.ExtraFeaturesService import org.mariotaku.twidere.util.premium.ExtraFeaturesService
import org.mariotaku.twidere.util.sync.DataSyncProvider import org.mariotaku.twidere.util.sync.DataSyncProvider
@ -38,8 +35,6 @@ open class BaseDialogFragment : DialogFragment(), IBaseFragment<BaseDialogFragme
@Inject @Inject
lateinit var keyboardShortcutsHandler: KeyboardShortcutsHandler lateinit var keyboardShortcutsHandler: KeyboardShortcutsHandler
@Inject @Inject
lateinit var dns: Dns
@Inject
lateinit var extraFeaturesService: ExtraFeaturesService lateinit var extraFeaturesService: ExtraFeaturesService
@Inject @Inject
lateinit var restHttpClient: RestHttpClient lateinit var restHttpClient: RestHttpClient

View File

@ -25,7 +25,6 @@ import android.support.v4.app.Fragment
import android.support.v4.text.BidiFormatter import android.support.v4.text.BidiFormatter
import android.view.View import android.view.View
import nl.komponents.kovenant.Promise import nl.komponents.kovenant.Promise
import okhttp3.Dns
import org.mariotaku.restfu.http.RestHttpClient import org.mariotaku.restfu.http.RestHttpClient
import org.mariotaku.twidere.dagger.component.GeneralComponent import org.mariotaku.twidere.dagger.component.GeneralComponent
import org.mariotaku.twidere.fragment.iface.IBaseFragment import org.mariotaku.twidere.fragment.iface.IBaseFragment
@ -62,8 +61,6 @@ open class BaseFragment : Fragment(), IBaseFragment<BaseFragment> {
@Inject @Inject
lateinit var restHttpClient: RestHttpClient lateinit var restHttpClient: RestHttpClient
@Inject @Inject
lateinit var dns: Dns
@Inject
lateinit var syncPreferences: SyncPreferences lateinit var syncPreferences: SyncPreferences
@Inject @Inject
lateinit var externalThemeManager: ExternalThemeManager lateinit var externalThemeManager: ExternalThemeManager

View File

@ -78,7 +78,7 @@ class HostMappingsListFragment : AbsContentListViewFragment<HostMappingsListFrag
when (item.itemId) { when (item.itemId) {
R.id.delete -> { R.id.delete -> {
val array = listView.checkedItemPositions ?: return false val array = listView.checkedItemPositions ?: return false
(dns as? TwidereDns)?.beginMappingTransaction { TwidereDns.get(context!!).beginMappingTransaction {
(0 until array.size()).filter { (0 until array.size()).filter {
array.valueAt(it) array.valueAt(it)
}.forEach { }.forEach {
@ -207,7 +207,7 @@ class HostMappingsListFragment : AbsContentListViewFragment<HostMappingsListFrag
} else { } else {
dialog.editAddress.string dialog.editAddress.string
}).takeUnless(String?::isNullOrEmpty) ?: return }).takeUnless(String?::isNullOrEmpty) ?: return
(dns as? TwidereDns)?.putMapping(host, address) TwidereDns.get(context!!).putMapping(host, address)
} }
override fun onSaveInstanceState(outState: Bundle) { override fun onSaveInstanceState(outState: Bundle) {

View File

@ -39,6 +39,7 @@ import org.mariotaku.twidere.extension.model.newMicroBlogInstance
import org.mariotaku.twidere.extension.restfu.headers import org.mariotaku.twidere.extension.restfu.headers
import org.mariotaku.twidere.extension.restfu.set import org.mariotaku.twidere.extension.restfu.set
import org.mariotaku.twidere.model.account.cred.OAuthCredentials import org.mariotaku.twidere.model.account.cred.OAuthCredentials
import org.mariotaku.twidere.singleton.PreferencesSingleton
import org.mariotaku.twidere.util.DataStoreUtils import org.mariotaku.twidere.util.DataStoreUtils
import org.mariotaku.twidere.util.MicroBlogAPIFactory import org.mariotaku.twidere.util.MicroBlogAPIFactory
import org.mariotaku.twidere.util.net.SystemDnsFetcher import org.mariotaku.twidere.util.net.SystemDnsFetcher
@ -106,9 +107,8 @@ class NetworkDiagnosticsFragment : BaseFragment() {
logPrintln("Text below may have personal information, BE CAREFUL TO MAKE IT PUBLIC", logPrintln("Text below may have personal information, BE CAREFUL TO MAKE IT PUBLIC",
LogText.State.WARNING) LogText.State.WARNING)
logPrintln() logPrintln()
val holder = DependencyHolder.get(context) val dns = TwidereDns.get(context)
val dns = holder.dns val prefs = PreferencesSingleton.get(context)
val prefs = holder.preferences
logPrintln(("Network preferences")) logPrintln(("Network preferences"))
logPrintln(("using_resolver: ${prefs.getBoolean(KEY_BUILTIN_DNS_RESOLVER, false)}")) logPrintln(("using_resolver: ${prefs.getBoolean(KEY_BUILTIN_DNS_RESOLVER, false)}"))
logPrintln(("tcp_dns_query: ${prefs.getBoolean(KEY_TCP_DNS_QUERY, false)}")) logPrintln(("tcp_dns_query: ${prefs.getBoolean(KEY_TCP_DNS_QUERY, false)}"))

View File

@ -25,7 +25,6 @@ import org.mariotaku.twidere.R
import org.mariotaku.twidere.annotation.TabAccountFlags import org.mariotaku.twidere.annotation.TabAccountFlags
import org.mariotaku.twidere.constant.IntentConstants.EXTRA_USER import org.mariotaku.twidere.constant.IntentConstants.EXTRA_USER
import org.mariotaku.twidere.constant.iWantMyStarsBackKey import org.mariotaku.twidere.constant.iWantMyStarsBackKey
import org.mariotaku.twidere.dagger.DependencyHolder
import org.mariotaku.twidere.fragment.timeline.FavoritesTimelineFragment import org.mariotaku.twidere.fragment.timeline.FavoritesTimelineFragment
import org.mariotaku.twidere.model.Tab import org.mariotaku.twidere.model.Tab
import org.mariotaku.twidere.model.tab.DrawableHolder import org.mariotaku.twidere.model.tab.DrawableHolder
@ -33,6 +32,7 @@ import org.mariotaku.twidere.model.tab.StringHolder
import org.mariotaku.twidere.model.tab.TabConfiguration import org.mariotaku.twidere.model.tab.TabConfiguration
import org.mariotaku.twidere.model.tab.argument.UserArguments import org.mariotaku.twidere.model.tab.argument.UserArguments
import org.mariotaku.twidere.model.tab.conf.UserExtraConfiguration import org.mariotaku.twidere.model.tab.conf.UserExtraConfiguration
import org.mariotaku.twidere.singleton.PreferencesSingleton
/** /**
* Created by mariotaku on 2016/11/27. * Created by mariotaku on 2016/11/27.
@ -67,7 +67,7 @@ class FavoriteTimelineTabConfiguration : TabConfiguration() {
object FavoriteStringHolder : StringHolder() { object FavoriteStringHolder : StringHolder() {
override fun createString(context: Context): String { override fun createString(context: Context): String {
if (DependencyHolder.get(context).preferences[iWantMyStarsBackKey]) { if (PreferencesSingleton.get(context)[iWantMyStarsBackKey]) {
return context.getString(R.string.title_favorites) return context.getString(R.string.title_favorites)
} }
return context.getString(R.string.title_likes) return context.getString(R.string.title_likes)

View File

@ -32,7 +32,6 @@ import android.os.Handler
import android.os.Looper import android.os.Looper
import android.os.Process import android.os.Process
import android.support.v4.text.BidiFormatter import android.support.v4.text.BidiFormatter
import okhttp3.Dns
import org.mariotaku.ktextension.isNullOrEmpty import org.mariotaku.ktextension.isNullOrEmpty
import org.mariotaku.ktextension.mapToArray import org.mariotaku.ktextension.mapToArray
import org.mariotaku.ktextension.toLongOr import org.mariotaku.ktextension.toLongOr
@ -69,8 +68,6 @@ class TwidereDataProvider : ContentProvider(), LazyLoadCallback {
@Inject @Inject
internal lateinit var notificationManager: NotificationManagerWrapper internal lateinit var notificationManager: NotificationManagerWrapper
@Inject @Inject
internal lateinit var dns: Dns
@Inject
internal lateinit var bidiFormatter: BidiFormatter internal lateinit var bidiFormatter: BidiFormatter
@Inject @Inject
internal lateinit var contentNotificationManager: ContentNotificationManager internal lateinit var contentNotificationManager: ContentNotificationManager

View File

@ -32,6 +32,7 @@ import org.mariotaku.twidere.constant.promotionsEnabledKey
import org.mariotaku.twidere.dagger.DependencyHolder import org.mariotaku.twidere.dagger.DependencyHolder
import org.mariotaku.twidere.model.UserKey import org.mariotaku.twidere.model.UserKey
import org.mariotaku.twidere.promise.MessagePromises import org.mariotaku.twidere.promise.MessagePromises
import org.mariotaku.twidere.singleton.PreferencesSingleton
import org.mariotaku.twidere.util.Utils import org.mariotaku.twidere.util.Utils
/** /**
@ -84,7 +85,7 @@ class NotificationReceiver : BroadcastReceiver() {
private fun setPromotionsEnabled(context: Context, intent: Intent, enabled: Boolean) { private fun setPromotionsEnabled(context: Context, intent: Intent, enabled: Boolean) {
val holder = DependencyHolder.get(context) val holder = DependencyHolder.get(context)
holder.preferences[promotionsEnabledKey] = enabled PreferencesSingleton.get(context)[promotionsEnabledKey] = enabled
val notificationId = intent.getIntExtra(EXTRA_NOTIFICATION_ID, -1) val notificationId = intent.getIntExtra(EXTRA_NOTIFICATION_ID, -1)
if (notificationId != -1) { if (notificationId != -1) {
holder.notificationManager.cancel(notificationId) holder.notificationManager.cancel(notificationId)

View File

@ -0,0 +1,13 @@
package org.mariotaku.twidere.singleton
import okhttp3.Cache
import org.mariotaku.twidere.constant.SharedPreferenceConstants
import org.mariotaku.twidere.extension.getCacheDir
import org.mariotaku.twidere.util.lang.ApplicationContextSingletonHolder
object CacheSingleton : ApplicationContextSingletonHolder<Cache>(creator@{
val preferences = PreferencesSingleton.get(it)
val cacheSizeMB = preferences.getInt(SharedPreferenceConstants.KEY_CACHE_SIZE_LIMIT, 300).coerceIn(100..500)
// Convert to bytes
return@creator Cache(it.getCacheDir("network", cacheSizeMB * 1048576L), cacheSizeMB * 1048576L)
})

View File

@ -478,4 +478,5 @@ object Utils {
context.applicationContext.sendBroadcast(intent) context.applicationContext.sendBroadcast(intent)
} }
} }

View File

@ -33,10 +33,13 @@ import okhttp3.Request
import org.mariotaku.twidere.dagger.DependencyHolder import org.mariotaku.twidere.dagger.DependencyHolder
import org.mariotaku.twidere.model.media.AuthenticatedUri import org.mariotaku.twidere.model.media.AuthenticatedUri
import org.mariotaku.twidere.model.media.NoThumborUrl import org.mariotaku.twidere.model.media.NoThumborUrl
import org.mariotaku.twidere.singleton.CacheSingleton
import org.mariotaku.twidere.singleton.PreferencesSingleton
import org.mariotaku.twidere.util.HttpClientFactory import org.mariotaku.twidere.util.HttpClientFactory
import org.mariotaku.twidere.util.UserAgentUtils import org.mariotaku.twidere.util.UserAgentUtils
import org.mariotaku.twidere.util.glide.NoThumborUrlLoader.Companion.HEADER_NO_THUMBOR import org.mariotaku.twidere.util.glide.NoThumborUrlLoader.Companion.HEADER_NO_THUMBOR
import org.mariotaku.twidere.util.media.ThumborWrapper import org.mariotaku.twidere.util.media.ThumborWrapper
import org.mariotaku.twidere.util.net.TwidereDns
import org.mariotaku.twidere.util.okhttp.ModifyRequestInterceptor import org.mariotaku.twidere.util.okhttp.ModifyRequestInterceptor
import java.io.InputStream import java.io.InputStream
@ -49,9 +52,11 @@ class TwidereGlideModule : AppGlideModule() {
override fun registerComponents(context: Context, glide: Glide, registry: Registry) { override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
val holder = DependencyHolder.get(context) val holder = DependencyHolder.get(context)
val builder = OkHttpClient.Builder() val builder = OkHttpClient.Builder()
val conf = HttpClientFactory.HttpClientConfiguration(holder.preferences) val conf = HttpClientFactory.HttpClientConfiguration(PreferencesSingleton.get(context))
val thumbor = holder.thumbor val thumbor = holder.thumbor
HttpClientFactory.initOkHttpClient(conf, builder, holder.dns, holder.cache) val dns = TwidereDns.get(context)
val cache = CacheSingleton.get(context)
HttpClientFactory.initOkHttpClient(conf, builder, dns, cache)
val userAgent = try { val userAgent = try {
UserAgentUtils.getDefaultUserAgentStringSafe(context) UserAgentUtils.getDefaultUserAgentStringSafe(context)
} catch (e: Exception) { } catch (e: Exception) {

View File

@ -26,15 +26,18 @@ import android.util.TimingLogger
import okhttp3.Dns import okhttp3.Dns
import org.mariotaku.ktextension.toIntOr import org.mariotaku.ktextension.toIntOr
import org.mariotaku.twidere.BuildConfig import org.mariotaku.twidere.BuildConfig
import org.mariotaku.twidere.Constants.*
import org.mariotaku.twidere.TwidereConstants.HOST_MAPPING_PREFERENCES_NAME import org.mariotaku.twidere.TwidereConstants.HOST_MAPPING_PREFERENCES_NAME
import org.mariotaku.twidere.constant.SharedPreferenceConstants.* import org.mariotaku.twidere.singleton.PreferencesSingleton
import org.mariotaku.twidere.util.lang.ApplicationContextSingletonHolder
import org.mariotaku.twidere.util.preference.PreferenceChangeNotifier
import org.xbill.DNS.* import org.xbill.DNS.*
import java.io.IOException import java.io.IOException
import java.net.InetAddress import java.net.InetAddress
import java.net.UnknownHostException import java.net.UnknownHostException
import java.util.* import java.util.*
class TwidereDns(val context: Context, private val preferences: SharedPreferences) : Dns { class TwidereDns private constructor(val context: Context) : Dns {
private val hostMapping = context.getSharedPreferences(HOST_MAPPING_PREFERENCES_NAME, private val hostMapping = context.getSharedPreferences(HOST_MAPPING_PREFERENCES_NAME,
Context.MODE_PRIVATE) Context.MODE_PRIVATE)
@ -44,6 +47,11 @@ class TwidereDns(val context: Context, private val preferences: SharedPreference
private var useResolver: Boolean = false private var useResolver: Boolean = false
init { init {
PreferenceChangeNotifier.get(context).register(KEY_DNS_SERVER, KEY_TCP_DNS_QUERY,
KEY_BUILTIN_DNS_RESOLVER) {
reloadDnsSettings()
}
reloadDnsSettings() reloadDnsSettings()
} }
@ -75,6 +83,7 @@ class TwidereDns(val context: Context, private val preferences: SharedPreference
fun reloadDnsSettings() { fun reloadDnsSettings() {
this.resolver = null this.resolver = null
val preferences = PreferencesSingleton.get(context)
useResolver = preferences.getBoolean(KEY_BUILTIN_DNS_RESOLVER, false) useResolver = preferences.getBoolean(KEY_BUILTIN_DNS_RESOLVER, false)
} }
@ -197,9 +206,10 @@ class TwidereDns(val context: Context, private val preferences: SharedPreference
private fun getResolver(): Resolver { private fun getResolver(): Resolver {
return this.resolver ?: run { return this.resolver ?: run {
val preferences = PreferencesSingleton.get(context)
val tcp = preferences.getBoolean(KEY_TCP_DNS_QUERY, false) val tcp = preferences.getBoolean(KEY_TCP_DNS_QUERY, false)
val servers = preferences.getString(KEY_DNS_SERVER, null)?.split(';', ',', ' ') ?: val servers = preferences.getString(KEY_DNS_SERVER, null)?.split(';', ',', ' ')
SystemDnsFetcher.get(context) ?: SystemDnsFetcher.get(context)
val resolvers = servers?.mapNotNull { val resolvers = servers?.mapNotNull {
val segs = it.split("#", limit = 2) val segs = it.split("#", limit = 2)
if (segs.isEmpty()) return@mapNotNull null if (segs.isEmpty()) return@mapNotNull null
@ -232,7 +242,7 @@ class TwidereDns(val context: Context, private val preferences: SharedPreference
return listOf(resolved) return listOf(resolved)
} }
companion object { companion object : ApplicationContextSingletonHolder<TwidereDns>(::TwidereDns) {
private val RESOLVER_LOGTAG = "TwidereDns" private val RESOLVER_LOGTAG = "TwidereDns"

View File

@ -47,6 +47,7 @@ import org.mariotaku.twidere.fragment.BaseFragment
import org.mariotaku.twidere.model.ParcelableUser import org.mariotaku.twidere.model.ParcelableUser
import org.mariotaku.twidere.model.ParcelableUserList import org.mariotaku.twidere.model.ParcelableUserList
import org.mariotaku.twidere.model.UserKey import org.mariotaku.twidere.model.UserKey
import org.mariotaku.twidere.singleton.PreferencesSingleton
import org.mariotaku.twidere.util.IntentUtils import org.mariotaku.twidere.util.IntentUtils
import org.mariotaku.twidere.util.UserColorNameManager import org.mariotaku.twidere.util.UserColorNameManager
import org.mariotaku.twidere.util.glide.DeferredTarget import org.mariotaku.twidere.util.glide.DeferredTarget
@ -60,7 +61,7 @@ object ShortcutCreator {
fun user(context: Context, accountKey: UserKey?, user: ParcelableUser): Promise<ShortcutInfoCompat, Exception> { fun user(context: Context, accountKey: UserKey?, user: ParcelableUser): Promise<ShortcutInfoCompat, Exception> {
val holder = DependencyHolder.get(context) val holder = DependencyHolder.get(context)
val preferences = holder.preferences val preferences = PreferencesSingleton.get(context)
val userColorNameManager = UserColorNameManager.get(context) val userColorNameManager = UserColorNameManager.get(context)
val profileImageStyle = if (useAdaptiveIcon) ImageShapeStyle.SHAPE_RECTANGLE else preferences[profileImageStyleKey] val profileImageStyle = if (useAdaptiveIcon) ImageShapeStyle.SHAPE_RECTANGLE else preferences[profileImageStyleKey]
@ -84,7 +85,7 @@ object ShortcutCreator {
fun userFavorites(context: Context, accountKey: UserKey?, user: ParcelableUser): Promise<ShortcutInfoCompat, Exception> { fun userFavorites(context: Context, accountKey: UserKey?, user: ParcelableUser): Promise<ShortcutInfoCompat, Exception> {
val holder = DependencyHolder.get(context) val holder = DependencyHolder.get(context)
val preferences = holder.preferences val preferences = PreferencesSingleton.get(context)
val userColorNameManager = UserColorNameManager.get(context) val userColorNameManager = UserColorNameManager.get(context)
val launchIntent = IntentUtils.userFavorites(accountKey, user.key, val launchIntent = IntentUtils.userFavorites(accountKey, user.key,

View File

@ -42,6 +42,7 @@ import org.mariotaku.twidere.constant.themeBackgroundOptionKey
import org.mariotaku.twidere.dagger.DependencyHolder import org.mariotaku.twidere.dagger.DependencyHolder
import org.mariotaku.twidere.extension.view.measureChildIgnoringInsets import org.mariotaku.twidere.extension.view.measureChildIgnoringInsets
import org.mariotaku.twidere.graphic.drawable.userprofile.ActionBarDrawable import org.mariotaku.twidere.graphic.drawable.userprofile.ActionBarDrawable
import org.mariotaku.twidere.singleton.PreferencesSingleton
import org.mariotaku.twidere.util.ThemeUtils import org.mariotaku.twidere.util.ThemeUtils
internal class HeaderBehavior(context: Context, attrs: AttributeSet? = null) : internal class HeaderBehavior(context: Context, attrs: AttributeSet? = null) :
@ -54,7 +55,7 @@ internal class HeaderBehavior(context: Context, attrs: AttributeSet? = null) :
init { init {
cardBackgroundColor = if (context.hasApplication) { cardBackgroundColor = if (context.hasApplication) {
val preferences = DependencyHolder.get(context).preferences val preferences = PreferencesSingleton.get(context)
ThemeUtils.getCardBackgroundColor(context, ThemeUtils.getCardBackgroundColor(context,
preferences[themeBackgroundOptionKey], preferences[themeBackgroundAlphaKey]) preferences[themeBackgroundOptionKey], preferences[themeBackgroundAlphaKey])
} else { } else {

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/send"
android:icon="@drawable/ic_action_send"
android:title="@string/action_send"
app:showAsAction="always" />
</menu>