add some initial things related to Google Sync, obtaining the account name

This commit is contained in:
tibbi 2017-01-29 21:26:42 +01:00
parent 1b06c20374
commit f285bee725
18 changed files with 116 additions and 6 deletions

View File

@ -36,6 +36,15 @@ dependencies {
compile 'joda-time:joda-time:2.9.1' compile 'joda-time:joda-time:2.9.1'
compile 'com.facebook.stetho:stetho:1.4.1' compile 'com.facebook.stetho:stetho:1.4.1'
compile 'com.bignerdranch.android:recyclerview-multiselect:0.2' compile 'com.bignerdranch.android:recyclerview-multiselect:0.2'
compile 'com.google.android.gms:play-services-auth:10.0.1'
compile('com.google.api-client:google-api-client-android:1.22.0') {
exclude group: 'org.apache.httpcomponents'
}
compile('com.google.apis:google-api-services-calendar:v3-rev230-1.22.0') {
exclude group: 'org.apache.httpcomponents'
}
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
} }

View File

@ -7,6 +7,9 @@
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.VIBRATE"/> <uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<application <application
android:name=".App" android:name=".App"
android:allowBackup="true" android:allowBackup="true"

View File

@ -1,14 +1,21 @@
package com.simplemobiletools.calendar.activities package com.simplemobiletools.calendar.activities
import android.app.Activity import android.Manifest
import android.accounts.AccountManager
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager
import android.media.RingtoneManager import android.media.RingtoneManager
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.os.Parcelable import android.os.Parcelable
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
import android.view.View import android.view.View
import android.widget.AdapterView import android.widget.AdapterView
import android.widget.ArrayAdapter import android.widget.ArrayAdapter
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential
import com.google.api.client.util.ExponentialBackOff
import com.google.api.services.calendar.CalendarScopes
import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.extensions.config import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.extensions.getDefaultReminderTypeIndex import com.simplemobiletools.calendar.extensions.getDefaultReminderTypeIndex
@ -21,11 +28,17 @@ import com.simplemobiletools.commons.extensions.*
import kotlinx.android.synthetic.main.activity_settings.* import kotlinx.android.synthetic.main.activity_settings.*
class SettingsActivity : SimpleActivity() { class SettingsActivity : SimpleActivity() {
val GET_RINGTONE_URI = 1 private val GET_RINGTONE_URI = 1
private val ACCOUNTS_PERMISSION = 2
private val REQUEST_ACCOUNT = 3
lateinit var credential: GoogleAccountCredential
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_settings) setContentView(R.layout.activity_settings)
credential = GoogleAccountCredential.usingOAuth2(this, arrayListOf(CalendarScopes.CALENDAR_READONLY)).setBackOff(ExponentialBackOff())
} }
override fun onResume() { override fun onResume() {
@ -33,6 +46,7 @@ class SettingsActivity : SimpleActivity() {
setupCustomizeColors() setupCustomizeColors()
setupSundayFirst() setupSundayFirst()
setupGoogleSync()
setupWeeklyStart() setupWeeklyStart()
setupWeeklyEnd() setupWeeklyEnd()
setupWeekNumbers() setupWeekNumbers()
@ -48,6 +62,20 @@ class SettingsActivity : SimpleActivity() {
} }
} }
private fun setupGoogleSync() {
settings_google_sync.isChecked = config.googleSync
settings_google_sync_holder.setOnClickListener {
settings_google_sync.toggle()
config.googleSync = settings_google_sync.isChecked
if (settings_google_sync.isChecked) {
if (config.syncAccountName.isEmpty()) {
checkGetAccountsPermission()
}
}
}
}
private fun setupSundayFirst() { private fun setupSundayFirst() {
settings_sunday_first.isChecked = config.isSundayFirst settings_sunday_first.isChecked = config.isSundayFirst
settings_sunday_first_holder.setOnClickListener { settings_sunday_first_holder.setOnClickListener {
@ -185,7 +213,7 @@ class SettingsActivity : SimpleActivity() {
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) { override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
if (resultCode == Activity.RESULT_OK && requestCode == GET_RINGTONE_URI) { if (resultCode == RESULT_OK && requestCode == GET_RINGTONE_URI) {
val uri = resultData?.getParcelableExtra<Parcelable>(RingtoneManager.EXTRA_RINGTONE_PICKED_URI) val uri = resultData?.getParcelableExtra<Parcelable>(RingtoneManager.EXTRA_RINGTONE_PICKED_URI)
if (uri == null) { if (uri == null) {
config.reminderSound = "" config.reminderSound = ""
@ -193,6 +221,34 @@ class SettingsActivity : SimpleActivity() {
settings_reminder_sound.text = RingtoneManager.getRingtone(this, uri as Uri).getTitle(this) settings_reminder_sound.text = RingtoneManager.getRingtone(this, uri as Uri).getTitle(this)
config.reminderSound = uri.toString() config.reminderSound = uri.toString()
} }
} else if (resultCode == RESULT_OK && requestCode == REQUEST_ACCOUNT && resultData?.extras != null) {
val accountName = resultData!!.getStringExtra(AccountManager.KEY_ACCOUNT_NAME)
config.syncAccountName = accountName
credential.selectedAccountName = accountName
}
}
private fun checkGetAccountsPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNTS) == PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.GET_ACCOUNTS), ACCOUNTS_PERMISSION)
} else {
showAccountChooser()
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == ACCOUNTS_PERMISSION) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
showAccountChooser()
}
}
}
private fun showAccountChooser() {
if (config.syncAccountName.isEmpty()) {
startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT)
} }
} }
} }

View File

@ -59,6 +59,14 @@ class Config(context: Context) : BaseConfig(context) {
prefs.edit().putInt(REMINDER_MINUTES, mins).apply() prefs.edit().putInt(REMINDER_MINUTES, mins).apply()
} }
var googleSync: Boolean
get() = prefs.getBoolean(GOOGLE_SYNC, false)
set(googleSync) = prefs.edit().putBoolean(GOOGLE_SYNC, googleSync).apply()
var syncAccountName: String
get() = prefs.getString(SYNC_ACCOUNT_NAME, "")
set(syncAccountName) = prefs.edit().putString(SYNC_ACCOUNT_NAME, syncAccountName).apply()
fun getDefaultNotificationSound(): String { fun getDefaultNotificationSound(): String {
try { try {
return RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_NOTIFICATION)?.toString() ?: "" return RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_NOTIFICATION)?.toString() ?: ""

View File

@ -39,6 +39,8 @@ val REMINDER_SOUND = "reminder_sound"
val VIEW = "view" val VIEW = "view"
val REMINDER_TYPE = "reminder_type" val REMINDER_TYPE = "reminder_type"
val REMINDER_MINUTES = "reminder_minutes" val REMINDER_MINUTES = "reminder_minutes"
val GOOGLE_SYNC = "google_sync"
val SYNC_ACCOUNT_NAME = "sync_account_name"
val letterIDs = intArrayOf(R.string.sunday_letter, R.string.monday_letter, R.string.tuesday_letter, R.string.wednesday_letter, val letterIDs = intArrayOf(R.string.sunday_letter, R.string.monday_letter, R.string.tuesday_letter, R.string.wednesday_letter,
R.string.thursday_letter, R.string.friday_letter, R.string.saturday_letter) R.string.thursday_letter, R.string.friday_letter, R.string.saturday_letter)

View File

@ -31,7 +31,7 @@
</RelativeLayout> </RelativeLayout>
<RelativeLayout <RelativeLayout
android:id="@+id/settings_sunday_first_holder" android:id="@+id/settings_google_sync_holder"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="@dimen/medium_margin" android:layout_marginTop="@dimen/medium_margin"
@ -39,14 +39,14 @@
android:padding="@dimen/activity_margin"> android:padding="@dimen/activity_margin">
<com.simplemobiletools.commons.views.MySwitchCompat <com.simplemobiletools.commons.views.MySwitchCompat
android:id="@+id/settings_sunday_first" android:id="@+id/settings_google_sync"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@null" android:background="@null"
android:clickable="false" android:clickable="false"
android:paddingLeft="@dimen/medium_margin" android:paddingLeft="@dimen/medium_margin"
android:paddingStart="@dimen/medium_margin" android:paddingStart="@dimen/medium_margin"
android:text="@string/sunday_first"/> android:text="@string/google_sync"/>
</RelativeLayout> </RelativeLayout>
@ -105,6 +105,26 @@
</RelativeLayout> </RelativeLayout>
<RelativeLayout
android:id="@+id/settings_sunday_first_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/medium_margin"
android:background="?attr/selectableItemBackground"
android:padding="@dimen/activity_margin">
<com.simplemobiletools.commons.views.MySwitchCompat
android:id="@+id/settings_sunday_first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:clickable="false"
android:paddingLeft="@dimen/medium_margin"
android:paddingStart="@dimen/medium_margin"
android:text="@string/sunday_first"/>
</RelativeLayout>
<RelativeLayout <RelativeLayout
android:id="@+id/settings_start_weekly_at_holder" android:id="@+id/settings_start_weekly_at_holder"
android:layout_width="match_parent" android:layout_width="match_parent"

View File

@ -73,6 +73,7 @@
<string name="no_ringtone_picker">No app capable of setting ringtone found</string> <string name="no_ringtone_picker">No app capable of setting ringtone found</string>
<string name="no_ringtone_selected">Kein Klingelton gewählt</string> <string name="no_ringtone_selected">Kein Klingelton gewählt</string>
<string name="day_end_before_start">The day cannot end earlier than it starts</string> <string name="day_end_before_start">The day cannot end earlier than it starts</string>
<string name="google_sync">Google sync</string>
<plurals name="events_deleted"> <plurals name="events_deleted">
<item quantity="one">1 Termin gelöscht</item> <item quantity="one">1 Termin gelöscht</item>

View File

@ -73,6 +73,7 @@
<string name="no_ringtone_picker">No app capable of setting ringtone found</string> <string name="no_ringtone_picker">No app capable of setting ringtone found</string>
<string name="no_ringtone_selected">None</string> <string name="no_ringtone_selected">None</string>
<string name="day_end_before_start">The day cannot end earlier than it starts</string> <string name="day_end_before_start">The day cannot end earlier than it starts</string>
<string name="google_sync">Google sync</string>
<plurals name="events_deleted"> <plurals name="events_deleted">
<item quantity="one">1 evento borrado</item> <item quantity="one">1 evento borrado</item>

View File

@ -73,6 +73,7 @@
<string name="no_ringtone_picker">Aucune application capable de configurer la sonnerie trouvée</string> <string name="no_ringtone_picker">Aucune application capable de configurer la sonnerie trouvée</string>
<string name="no_ringtone_selected">None</string> <string name="no_ringtone_selected">None</string>
<string name="day_end_before_start">The day cannot end earlier than it starts</string> <string name="day_end_before_start">The day cannot end earlier than it starts</string>
<string name="google_sync">Google sync</string>
<plurals name="events_deleted"> <plurals name="events_deleted">
<item quantity="one">1 événement supprimé</item> <item quantity="one">1 événement supprimé</item>

View File

@ -73,6 +73,7 @@
<string name="no_ringtone_picker">No app capable of setting ringtone found</string> <string name="no_ringtone_picker">No app capable of setting ringtone found</string>
<string name="no_ringtone_selected">None</string> <string name="no_ringtone_selected">None</string>
<string name="day_end_before_start">The day cannot end earlier than it starts</string> <string name="day_end_before_start">The day cannot end earlier than it starts</string>
<string name="google_sync">Google sync</string>
<plurals name="events_deleted"> <plurals name="events_deleted">
<item quantity="one">1 इवेंट डिलीट किया गया</item> <item quantity="one">1 इवेंट डिलीट किया गया</item>

View File

@ -73,6 +73,7 @@
<string name="no_ringtone_picker">No app capable of setting ringtone found</string> <string name="no_ringtone_picker">No app capable of setting ringtone found</string>
<string name="no_ringtone_selected">None</string> <string name="no_ringtone_selected">None</string>
<string name="day_end_before_start">The day cannot end earlier than it starts</string> <string name="day_end_before_start">The day cannot end earlier than it starts</string>
<string name="google_sync">Google sync</string>
<plurals name="events_deleted"> <plurals name="events_deleted">
<item quantity="one">1 bejegyzés törölve</item> <item quantity="one">1 bejegyzés törölve</item>

View File

@ -73,6 +73,7 @@
<string name="no_ringtone_picker">No app capable of setting ringtone found</string> <string name="no_ringtone_picker">No app capable of setting ringtone found</string>
<string name="no_ringtone_selected">None</string> <string name="no_ringtone_selected">None</string>
<string name="day_end_before_start">The day cannot end earlier than it starts</string> <string name="day_end_before_start">The day cannot end earlier than it starts</string>
<string name="google_sync">Google sync</string>
<plurals name="events_deleted"> <plurals name="events_deleted">
<item quantity="one">1 evento eliminato</item> <item quantity="one">1 evento eliminato</item>

View File

@ -73,6 +73,7 @@
<string name="no_ringtone_picker">לא נמצאה אפליקציה להגדרת רינגטון</string> <string name="no_ringtone_picker">לא נמצאה אפליקציה להגדרת רינגטון</string>
<string name="no_ringtone_selected">ללא</string> <string name="no_ringtone_selected">ללא</string>
<string name="day_end_before_start">היום לא יכול להסתיים מוקדם משהוא מתחיל</string> <string name="day_end_before_start">היום לא יכול להסתיים מוקדם משהוא מתחיל</string>
<string name="google_sync">Google sync</string>
<plurals name="events_deleted"> <plurals name="events_deleted">
<item quantity="one">אירוע אחד נמחק</item> <item quantity="one">אירוע אחד נמחק</item>

View File

@ -73,6 +73,7 @@
<string name="no_ringtone_picker">No app capable of setting ringtone found</string> <string name="no_ringtone_picker">No app capable of setting ringtone found</string>
<string name="no_ringtone_selected">None</string> <string name="no_ringtone_selected">None</string>
<string name="day_end_before_start">The day cannot end earlier than it starts</string> <string name="day_end_before_start">The day cannot end earlier than it starts</string>
<string name="google_sync">Google sync</string>
<plurals name="events_deleted"> <plurals name="events_deleted">
<item quantity="one">1 イベントを削除しました</item> <item quantity="one">1 イベントを削除しました</item>

View File

@ -73,6 +73,7 @@
<string name="no_ringtone_picker">Não foi encontrada qualquer aplicação capaz de definir o som</string> <string name="no_ringtone_picker">Não foi encontrada qualquer aplicação capaz de definir o som</string>
<string name="no_ringtone_selected">Nenhum</string> <string name="no_ringtone_selected">Nenhum</string>
<string name="day_end_before_start">O dia final não pode ser antes do inicial</string> <string name="day_end_before_start">O dia final não pode ser antes do inicial</string>
<string name="google_sync">Google sync</string>
<plurals name="events_deleted"> <plurals name="events_deleted">
<item quantity="one">1 evento apagado</item> <item quantity="one">1 evento apagado</item>

View File

@ -73,6 +73,7 @@
<string name="no_ringtone_picker">Не найдено приложение для выбора рингтона</string> <string name="no_ringtone_picker">Не найдено приложение для выбора рингтона</string>
<string name="no_ringtone_selected">Нет</string> <string name="no_ringtone_selected">Нет</string>
<string name="day_end_before_start">The day cannot end earlier than it starts</string> <string name="day_end_before_start">The day cannot end earlier than it starts</string>
<string name="google_sync">Google sync</string>
<plurals name="events_deleted"> <plurals name="events_deleted">
<item quantity="one">1 событие удалено</item> <item quantity="one">1 событие удалено</item>

View File

@ -73,6 +73,7 @@
<string name="no_ringtone_picker">Hittade ingen app som kan ställa in ljudet</string> <string name="no_ringtone_picker">Hittade ingen app som kan ställa in ljudet</string>
<string name="no_ringtone_selected">Ingen</string> <string name="no_ringtone_selected">Ingen</string>
<string name="day_end_before_start">Dagen kan inte börja innan den tar slut</string> <string name="day_end_before_start">Dagen kan inte börja innan den tar slut</string>
<string name="google_sync">Google sync</string>
<plurals name="events_deleted"> <plurals name="events_deleted">
<item quantity="one">1 händelse borttagen</item> <item quantity="one">1 händelse borttagen</item>

View File

@ -73,6 +73,7 @@
<string name="no_ringtone_picker">No app capable of setting ringtone found</string> <string name="no_ringtone_picker">No app capable of setting ringtone found</string>
<string name="no_ringtone_selected">None</string> <string name="no_ringtone_selected">None</string>
<string name="day_end_before_start">The day cannot end earlier than it starts</string> <string name="day_end_before_start">The day cannot end earlier than it starts</string>
<string name="google_sync">Google sync</string>
<plurals name="events_deleted"> <plurals name="events_deleted">
<item quantity="one">1 event deleted</item> <item quantity="one">1 event deleted</item>