diff --git a/twidere/src/google/kotlin/org/mariotaku/twidere/VariantConstants.kt b/twidere/src/google/kotlin/org/mariotaku/twidere/VariantConstants.kt deleted file mode 100644 index 61e772fce..000000000 --- a/twidere/src/google/kotlin/org/mariotaku/twidere/VariantConstants.kt +++ /dev/null @@ -1,8 +0,0 @@ -package org.mariotaku.twidere - -import org.mariotaku.kpreferences.KNullableStringKey - -/** - * Created by mariotaku on 2016/12/28. - */ -val dropboxAuthTokenKey = KNullableStringKey("dropbox_auth_token", null) \ No newline at end of file diff --git a/twidere/src/google/kotlin/org/mariotaku/twidere/activity/DropboxAuthStarterActivity.kt b/twidere/src/google/kotlin/org/mariotaku/twidere/activity/DropboxAuthStarterActivity.kt index 316acbfd4..44e04852a 100644 --- a/twidere/src/google/kotlin/org/mariotaku/twidere/activity/DropboxAuthStarterActivity.kt +++ b/twidere/src/google/kotlin/org/mariotaku/twidere/activity/DropboxAuthStarterActivity.kt @@ -4,25 +4,35 @@ import android.os.Bundle import com.dropbox.core.android.Auth import org.mariotaku.kpreferences.set import org.mariotaku.twidere.Constants.DROPBOX_APP_KEY -import org.mariotaku.twidere.dropboxAuthTokenKey +import org.mariotaku.twidere.constant.dataSyncProviderInfoKey +import org.mariotaku.twidere.model.sync.DropboxSyncProviderInfo /** * Created by mariotaku on 2016/12/7. */ class DropboxAuthStarterActivity : BaseActivity() { + private var shouldGetAuthResult: Boolean = false + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) Auth.startOAuth2Authentication(this, DROPBOX_APP_KEY) - } override fun onResume() { super.onResume() - val oauthToken = Auth.getOAuth2Token() - if (oauthToken != null) { - preferences[dropboxAuthTokenKey] = oauthToken + if (shouldGetAuthResult) { + val oauthToken = Auth.getOAuth2Token() + if (oauthToken != null) { + preferences[dataSyncProviderInfoKey] = DropboxSyncProviderInfo(oauthToken) + } + finish() + shouldGetAuthResult = false } - finish() + } + + override fun onPause() { + super.onPause() + shouldGetAuthResult = true } } \ No newline at end of file diff --git a/twidere/src/google/kotlin/org/mariotaku/twidere/fragment/premium/PlayStoreExtraFeaturesSyncStatusFragment.kt b/twidere/src/google/kotlin/org/mariotaku/twidere/fragment/premium/PlayStoreExtraFeaturesSyncStatusFragment.kt deleted file mode 100644 index 47065214f..000000000 --- a/twidere/src/google/kotlin/org/mariotaku/twidere/fragment/premium/PlayStoreExtraFeaturesSyncStatusFragment.kt +++ /dev/null @@ -1,56 +0,0 @@ -package org.mariotaku.twidere.fragment.premium - -import android.content.Intent -import android.os.Bundle -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import kotlinx.android.synthetic.google.fragment_extra_features_sync_status_play_store.* -import org.mariotaku.kpreferences.get -import org.mariotaku.twidere.R -import org.mariotaku.twidere.activity.DropboxAuthStarterActivity -import org.mariotaku.twidere.dropboxAuthTokenKey -import org.mariotaku.twidere.fragment.BaseSupportFragment -import org.mariotaku.twidere.service.DropboxDataSyncService - -/** - * Created by mariotaku on 2016/12/28. - */ - -class PlayStoreExtraFeaturesSyncStatusFragment : BaseSupportFragment() { - private val REQUEST_DROPBOX_AUTH: Int = 201 - - override fun onActivityCreated(savedInstanceState: Bundle?) { - super.onActivityCreated(savedInstanceState) - updateButtons() - connectStorageService.setOnClickListener { - startActivityForResult(Intent(context, DropboxAuthStarterActivity::class.java), REQUEST_DROPBOX_AUTH) - } - performSync.setOnClickListener { - context.startService(Intent(context, DropboxDataSyncService::class.java)) - } - } - - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - when (requestCode) { - REQUEST_DROPBOX_AUTH -> { - updateButtons() - } - } - } - - private fun updateButtons() { - if (preferences[dropboxAuthTokenKey] == null) { - connectStorageService.visibility = View.VISIBLE - performSync.visibility = View.GONE - } else { - connectStorageService.visibility = View.GONE - performSync.visibility = View.VISIBLE - - } - } - - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { - return inflater.inflate(R.layout.fragment_extra_features_sync_status_play_store, container, false) - } -} diff --git a/twidere/src/google/kotlin/org/mariotaku/twidere/model/sync/DropboxSyncProviderInfo.kt b/twidere/src/google/kotlin/org/mariotaku/twidere/model/sync/DropboxSyncProviderInfo.kt new file mode 100644 index 000000000..ea75e4db0 --- /dev/null +++ b/twidere/src/google/kotlin/org/mariotaku/twidere/model/sync/DropboxSyncProviderInfo.kt @@ -0,0 +1,25 @@ +package org.mariotaku.twidere.model.sync + +import android.content.SharedPreferences + +/** + * Created by mariotaku on 2017/1/2. + */ + +class DropboxSyncProviderInfo(val authToken: String) : SyncProviderInfo(DropboxSyncProviderInfo.TYPE) { + + override fun writeToPreferences(editor: SharedPreferences.Editor) { + editor.putString(KEY_DROPBOX_AUTH_TOKEN, authToken) + } + + companion object { + const val TYPE = "dropbox" + + private const val KEY_DROPBOX_AUTH_TOKEN = "dropbox_auth_token" + fun newInstance(preferences: SharedPreferences): DropboxSyncProviderInfo? { + val authToken = preferences.getString(KEY_DROPBOX_AUTH_TOKEN, null) ?: return null + return DropboxSyncProviderInfo(authToken) + } + } + +} diff --git a/twidere/src/google/kotlin/org/mariotaku/twidere/service/DropboxDataSyncService.kt b/twidere/src/google/kotlin/org/mariotaku/twidere/service/DropboxDataSyncService.kt index 00f00db20..703f4146c 100644 --- a/twidere/src/google/kotlin/org/mariotaku/twidere/service/DropboxDataSyncService.kt +++ b/twidere/src/google/kotlin/org/mariotaku/twidere/service/DropboxDataSyncService.kt @@ -15,10 +15,11 @@ import com.dropbox.core.v2.files.* import org.mariotaku.kpreferences.get import org.mariotaku.twidere.BuildConfig import org.mariotaku.twidere.R -import org.mariotaku.twidere.dropboxAuthTokenKey +import org.mariotaku.twidere.constant.dataSyncProviderInfoKey import org.mariotaku.twidere.extension.model.* import org.mariotaku.twidere.model.Draft import org.mariotaku.twidere.model.FiltersData +import org.mariotaku.twidere.model.sync.DropboxSyncProviderInfo import org.mariotaku.twidere.util.sync.* import org.xmlpull.v1.XmlPullParser import org.xmlpull.v1.XmlSerializer @@ -33,7 +34,7 @@ class DropboxDataSyncService : BaseIntentService("dropbox_data_sync") { private val NOTIFICATION_ID_SYNC_DATA = 302 override fun onHandleIntent(intent: Intent?) { - val authToken = preferences[dropboxAuthTokenKey] ?: return + val syncInfo = preferences[dataSyncProviderInfoKey] as? DropboxSyncProviderInfo ?: return val nb = NotificationCompat.Builder(this) nb.setSmallIcon(R.drawable.ic_stat_refresh) nb.setOngoing(true) @@ -43,7 +44,7 @@ class DropboxDataSyncService : BaseIntentService("dropbox_data_sync") { nm.notify(NOTIFICATION_ID_SYNC_DATA, nb.build()) val requestConfig = DbxRequestConfig.newBuilder("twidere-android/${BuildConfig.VERSION_NAME}") .build() - val client = DbxClientV2(requestConfig, authToken) + val client = DbxClientV2(requestConfig, syncInfo.authToken) arrayOf( DropboxDraftsSyncHelper(this, client), DropboxFiltersDataSyncHelper(this, client), diff --git a/twidere/src/google/kotlin/org/mariotaku/twidere/util/FabricAnalyzer.kt b/twidere/src/google/kotlin/org/mariotaku/twidere/util/FabricAnalyzer.kt index db235790e..891412652 100644 --- a/twidere/src/google/kotlin/org/mariotaku/twidere/util/FabricAnalyzer.kt +++ b/twidere/src/google/kotlin/org/mariotaku/twidere/util/FabricAnalyzer.kt @@ -111,7 +111,7 @@ class FabricAnalyzer : Analyzer(), Constants { private fun AnswersEvent<*>.putAttributes(event: Analyzer.Event) { if (event.accountType != null) { - putCustomAttribute("Account type", event.accountType ?: "unknown") + putCustomAttribute("Account type", event.accountType) } if (event.accountHost != null) { putCustomAttribute("Account host", event.accountHost) diff --git a/twidere/src/google/kotlin/org/mariotaku/twidere/util/sync/NonFreeSyncProviderInfoFactory.kt b/twidere/src/google/kotlin/org/mariotaku/twidere/util/sync/NonFreeSyncProviderInfoFactory.kt new file mode 100644 index 000000000..f8a9ca999 --- /dev/null +++ b/twidere/src/google/kotlin/org/mariotaku/twidere/util/sync/NonFreeSyncProviderInfoFactory.kt @@ -0,0 +1,31 @@ +package org.mariotaku.twidere.util.sync + +import android.content.Context +import android.content.Intent +import android.content.SharedPreferences +import org.mariotaku.twidere.R +import org.mariotaku.twidere.activity.DropboxAuthStarterActivity +import org.mariotaku.twidere.model.sync.DropboxSyncProviderInfo +import org.mariotaku.twidere.model.sync.SyncProviderEntry +import org.mariotaku.twidere.model.sync.SyncProviderInfo + +/** + * Created by mariotaku on 2017/1/2. + */ + +class NonFreeSyncProviderInfoFactory : SyncProviderInfoFactory() { + override fun getInfoForType(type: String, preferences: SharedPreferences): SyncProviderInfo? { + return when (type) { + DropboxSyncProviderInfo.TYPE -> DropboxSyncProviderInfo.newInstance(preferences) + else -> null + } + } + + override fun getSupportedProviders(context: Context): List { + return listOf( + SyncProviderEntry(DropboxSyncProviderInfo.TYPE, + context.getString(R.string.sync_provider_name_dropbox), + Intent(context, DropboxAuthStarterActivity::class.java)) + ) + } +} diff --git a/twidere/src/google/res/layout/card_item_extra_features_sync_status.xml b/twidere/src/google/res/layout/card_item_extra_features_sync_status.xml index 7069ecfbe..ed89011a3 100644 --- a/twidere/src/google/res/layout/card_item_extra_features_sync_status.xml +++ b/twidere/src/google/res/layout/card_item_extra_features_sync_status.xml @@ -9,8 +9,8 @@ + tools:layout="@layout/fragment_extra_features_sync_status"/> \ No newline at end of file diff --git a/twidere/src/google/res/layout/fragment_extra_features_sync_status.xml b/twidere/src/google/res/layout/fragment_extra_features_sync_status.xml new file mode 100644 index 000000000..30b7e28ee --- /dev/null +++ b/twidere/src/google/res/layout/fragment_extra_features_sync_status.xml @@ -0,0 +1,62 @@ + + + + + + + + + + + +