ともちゃんのAndroid開発日記

組込みC言語プログラマだったともちゃんが、四苦八苦しながら、AndroidのJAVAとKotlinを習得して行きます。ともちゃんの備忘録も兼ねています。

Googleカレンダーのイベントを削除する

Googleカレンダーは便利なのですが、カレンダーのイベントを自動的にあるいは一括して削除する機能がないので、一括して削除する機能を作ってみました。

AndroidManifest.xml(アカウントにアクセスするパミッション)

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />

アカウントダイアログを出して、ユーザにGoogleアカウントを選択させる。

そのアカウントを認証する。

f:id:uchida001tmhr:20181020212124p:plain

MainActivity.kt

private fun sendPermission() {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

if (ContextCompat.checkSelfPermission(this,
Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {

if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.GET_ACCOUNTS)) {
} else {
if (snackBar == null) {
val dataCenter = DataCenter()
snackBar = Snackbar.make(dataCenter.getListView()!!, R.string.Processing, Snackbar.LENGTH_INDEFINITE)
val snackView = snackBar!!.view as Snackbar.SnackbarLayout
val progressBar = ProgressBar(this, null, android.R.attr.progressBarStyleSmall)
snackView.addView(progressBar)
snackBar!!.show()
}
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.GET_ACCOUNTS),
REQUEST_PERMISSION_GET_ACCOUNTS)
}
} else {
if (snackBar == null) {
val dataCenter = DataCenter()
snackBar = Snackbar.make(dataCenter.getListView()!!, R.string.Processing, Snackbar.LENGTH_INDEFINITE)
val snackView = snackBar!!.view as Snackbar.SnackbarLayout
val progressBar = ProgressBar(this, null, android.R.attr.progressBarStyleSmall)
snackView.addView(progressBar)
snackBar!!.show()
}
// Googleアカウントの選択を表示する
// GoogleAccountCredentialのアカウント選択画面を使用する
mCredential = GoogleAccountCredential.usingOAuth2(this, Arrays.asList(SCOPES)).setBackOff(ExponentialBackOff())
startActivityForResult(
mCredential!!.newChooseAccountIntent(),
REQUEST_ACCOUNT_PICKER)
}
} else {
if (snackBar == null) {
val dataCenter = DataCenter()
snackBar = Snackbar.make(dataCenter.getListView()!!, R.string.Processing, Snackbar.LENGTH_INDEFINITE)
val snackView = snackBar!!.view as Snackbar.SnackbarLayout
val progressBar = ProgressBar(this, null, android.R.attr.progressBarStyleSmall)
snackView.addView(progressBar)
snackBar!!.show()
}
// Googleアカウントの選択を表示する
// GoogleAccountCredentialのアカウント選択画面を使用する
mCredential = GoogleAccountCredential.usingOAuth2(this, Arrays.asList(SCOPES)).setBackOff(ExponentialBackOff())
startActivityForResult(
mCredential!!.newChooseAccountIntent(),
REQUEST_ACCOUNT_PICKER)
}
}

override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>,
grantResults: IntArray
) {
if (snackBar == null) {
val dataCenter = DataCenter()
snackBar = Snackbar.make(dataCenter.getListView()!!, R.string.Processing, Snackbar.LENGTH_INDEFINITE)
val snackView = snackBar!!.view as Snackbar.SnackbarLayout
val progressBar = ProgressBar(this, null, android.R.attr.progressBarStyleSmall)
snackView.addView(progressBar)
snackBar!!.show()
}
// Googleアカウントの選択を表示する
// GoogleAccountCredentialのアカウント選択画面を使用する
mCredential = GoogleAccountCredential.usingOAuth2(this, Arrays.asList(SCOPES)).setBackOff(ExponentialBackOff())
startActivityForResult(
mCredential!!.newChooseAccountIntent(),
REQUEST_ACCOUNT_PICKER)
}

private fun authenticate(account : String?) {
val manager = AccountManager.get(this)
manager.getAuthToken(Account(account, "com.google"),
//"oauth2:https://www.googleapis.com/auth/calendar.events",
"oauth2:https://www.googleapis.com/auth/calendar",
null,
false,
AccountManagerCallback { accountManagerFuture ->
try {
val bundle = accountManagerFuture.result
val intent = bundle.get(AccountManager.KEY_INTENT) as Intent?
if (intent == null) {
val authToken = bundle.get(AccountManager.KEY_AUTHTOKEN)!!.toString()
val databaseManager = DatabaseManager()
databaseManager.replaceAccountDatabaseAuthToken(account!!, authToken)

if (snackBar!=null && snackBar!!.isShown) {
snackBar!!.dismiss()
snackBar = null
}
} else {
// 認証許可ダイアログを表示
startActivityForResult(intent, REQUEST_AUTH_DIALOG)
}
} catch (e: OperationCanceledException) {
e.printStackTrace()
} catch (e: IOException) {
e.printStackTrace()
} catch (e: AuthenticatorException) {
e.printStackTrace()
}
},
null)
}

override fun onActivityResult(
requestCode: Int,
resultCode: Int,
data: Intent?
) {
when (requestCode) {
REQUEST_ACCOUNT_PICKER -> {
if (resultCode == RESULT_OK && data != null && data.getExtras() != null) {
val accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME)
pendingAccount = accountName
val databaseManager = DatabaseManager()
val cursor = databaseManager.queryAccountDatabase(accountName)
if (!cursor.moveToFirst()) {
databaseManager.insertAccountDatabase(accountName, databaseManager.DB_TRUE, DEFAULT_SHELF_LIFE, "")
}
updateList()
authenticate(accountName)
} else {
if (snackBar!=null && snackBar!!.isShown) {
snackBar!!.dismiss()
snackBar = null
}
}
}
REQUEST_AUTH_DIALOG -> {
if (resultCode == RESULT_OK) {
authenticate(pendingAccount)
pendingAccount = null
} else {
if (snackBar!=null && snackBar!!.isShown) {
snackBar!!.dismiss()
snackBar = null
}
}
}
REQUEST_AUTH_AGAIN -> {
if (resultCode == RESULT_OK) {
}
}
}
}

Googleカレンダーのイベント削除処理

MainActivity.kt

fun deleteCalendarEvent(context : Context, progressBar : ProgressBar) {

var mService : com.google.api.services.calendar.Calendar

val databaseManager = DatabaseManager()
val cursor = databaseManager.queryAccountDatabase()
if (cursor.moveToFirst()) {
do {

try {
val checked = cursor.getLong(cursor.getColumnIndex("checked"))
if (checked == databaseManager.DB_TRUE) {

val account = cursor.getString((cursor.getColumnIndex("account")))

mCredential = GoogleAccountCredential.usingOAuth2(context, Arrays.asList(SCOPES)).setBackOff(ExponentialBackOff())

mCredential!!.setSelectedAccountName(account)
val transport = AndroidHttp.newCompatibleTransport()
val jsonFactory = JacksonFactory.getDefaultInstance()
mService = com.google.api.services.calendar.Calendar.Builder(transport, jsonFactory, mCredential)
.build()

val shelfLife = cursor.getInt(cursor.getColumnIndex("shelf_life"))
val timeZone = TimeZone.getDefault()
val calendarToDelete = Calendar.getInstance(timeZone)
val year = calendarToDelete.get(Calendar.YEAR)
val month = calendarToDelete.get(Calendar.MONTH)
val date = calendarToDelete.get(Calendar.DATE)
val hour = calendarToDelete.get(Calendar.HOUR_OF_DAY)
val minute = calendarToDelete.get(Calendar.MINUTE)
val second = calendarToDelete.get(Calendar.SECOND)
val strYear = String.format("%04d", year)
val strMonth = String.format("%02d", month+1)
val strDate = String.format("%02d", date)
val strHour = String.format("%02d", hour)
val strMinute = String.format("%02d", minute)
val strSecond = String.format("%02d", second)
val maxDateTime1 = "$strYear-$strMonth-$strDate"
val maxDateTime2 = "$strHour:$strMinute:$strSecond"
val maxDateTime = DateTime.parseRfc3339(maxDateTime1 + "T" + maxDateTime2)
val events = mService.events().list(account)
.setTimeMax(maxDateTime)
.execute()
val eventItems = events.items
progressBar.max = eventItems.size
var i=1
for (event in eventItems) {
progressBar.progress = i
if (isDeletable(event, shelfLife, calendarToDelete, timeZone)) {
val eventId = event.id
mService.events().delete(account, eventId).execute()
}
i++
}
}

} catch (e: UserRecoverableAuthIOException) {
(context as Activity).startActivityForResult(e.intent, REQUEST_AUTH_AGAIN)
} catch (e: IOException) {
}

} while (cursor.moveToNext())
}
cursor.close()

}

private fun toRfc3339(calendar : Calendar) : String {
val year = calendar.get(Calendar.YEAR)
val month = calendar.get(Calendar.MONTH)
val date = calendar.get(Calendar.DATE)
val hour = calendar.get(Calendar.HOUR_OF_DAY)
val minute = calendar.get(Calendar.MINUTE)
val second = calendar.get(Calendar.SECOND)
val strYear = String.format("%04d", year)
val strMonth = String.format("%02d", month + 1)
val strDate = String.format("%02d", date)
val strHour = String.format("%02d", hour)
val strMinute = String.format("%02d", minute)
val strSecond = String.format("%02d", second)
val maxDateTime1 = "$strYear-$strMonth-$strDate"
val maxDateTime2 = "$strHour:$strMinute:$strSecond"
return DateTime.parseRfc3339(maxDateTime1 + "T" + maxDateTime2).toStringRfc3339()
}


private fun isDeletable(event : Event, shelfLife : Int, calendarNow: Calendar, timeZone: TimeZone) : Boolean {
val calendarToDelete = Calendar.getInstance(timeZone)
calendarToDelete.set(
calendarNow.get(Calendar.YEAR),
calendarNow.get(Calendar.MONTH),
calendarNow.get(Calendar.DATE),
calendarNow.get(Calendar.HOUR_OF_DAY),
calendarNow.get(Calendar.MINUTE),
calendarNow.get(Calendar.SECOND)
)
calendarToDelete.set(Calendar.MILLISECOND, 0)
calendarToDelete.add(Calendar.MONTH, -shelfLife)
if (event.endTimeUnspecified != null) {
if (event.endTimeUnspecified) return false
}
val calculateEndInRFC3339 = CalculateEndInRFC3339()
val strEventEnd : String? = calculateEndInRFC3339.calcEnd(event, timeZone)
if (strEventEnd == null) {
Log.d("GCDelete:strEventEnd","null")
return false
}
val strNow : String = toRfc3339(calendarToDelete)

return (strEventEnd.compareTo(strNow) < 0)
}

イベントの情報から、イベントの最終日を計算する処理

CalculateEndInRFC3339.kt

class CalculateEndInRFC3339 {

fun calcEnd(event : Event, timeZone: TimeZone) : String? {

var eventEnd : String? = null
if (event.recurrence == null) {
// 繰り返しではない
if (event.end != null) {
if (event.end.dateTime != null && event.end.date == null) {
// 終日ではない
eventEnd = event.end.dateTime.toStringRfc3339()
} else {
// 終日
eventEnd = event.end.date.toStringRfc3339()
}
} else {
}
} else {
// 繰り返しイベント
val calEventStart : Calendar = Calendar.getInstance(timeZone)
var strEventStart : String? = null
var eventStart : String? = null
if (event.start != null) {
if (event.start.dateTime != null && event.start.date == null) {
// 終日ではない
eventStart = event.start.dateTime.toStringRfc3339()
} else {
// 終日
eventStart = event.start.date.toStringRfc3339()
}
}
val pattern = Pattern.compile("[0-9]{4}-[0-9]{2}-[0-9]{2}")
val matcher = pattern.matcher(eventStart)
if (matcher.find()) {
strEventStart = matcher.group()
}
val locale = Locale.getDefault()
val sdf = SimpleDateFormat("yyyy-MM-dd",locale)
var date : Date? = null
try {
date = sdf.parse(strEventStart)
} catch (e: ParseException) {
return null
}
calEventStart.time = date

for (it in event.recurrence) {
if (isRRULE(it)) {
eventEnd = analyzeRRULE(it, calEventStart)
}
}
}
return eventEnd
}

private fun isRRULE(it : String) : Boolean {
val pattern = Pattern.compile("^RRULE")
val matcher = pattern.matcher(it)
return matcher.find()
}

private fun analyzeRRULE(it : String, calStart: Calendar) : String? {

var resultEndDate : String? = null

// UNTIL
val pattern01 = Pattern.compile("UNTIL=[0-9]{4}[0-9]{2}[0-9]{2}")
val matcher01 = pattern01.matcher(it)
if (matcher01.find()) {
val strBuffer = matcher01.group()
val pattern02 = Pattern.compile("[0-9]{4}[0-9]{2}[0-9]{2}")
val matcher02 = pattern02.matcher(strBuffer)
if (matcher02.find()) {
val strDate = matcher02.group()
val locale = Locale.getDefault()
val sdf = SimpleDateFormat("yyyyMMdd",locale)
var dateTemp : Date? = null
try {
dateTemp = sdf.parse(strDate)
} catch (e: ParseException) {
return null
}
val calEnd = Calendar.getInstance(calStart.timeZone)
calEnd.time = dateTemp
val year=String.format("%04d", calEnd.get(Calendar.YEAR))
val month=String.format("%02d", calEnd.get(Calendar.MONTH)+1)
val date=String.format("%02d", calEnd.get(Calendar.DATE))
resultEndDate = "$year-$month-$date"

}
}

// COUNT, INTERVAL
var count = 0
var countFlag = false
var interval = 0
var intervalFlag = false
var daily = false
var weekly = false
var monthly = false
var yearly= false
val pattern03 = Pattern.compile("COUNT=[0-9]*")
val matcher03 = pattern03.matcher(it)
if (matcher03.find()) {
val strBuffer = matcher03.group()
val pattern04 = Pattern.compile("[0-9]+")
val matcher04 = pattern04.matcher(strBuffer)
if (matcher04.find()) {
val strCount = matcher04.group()
count = strCount.toInt()
countFlag = true
}
}
val pattern05 = Pattern.compile("INTERVAL=[0-9]*")
val matcher05 = pattern05.matcher(it)
if (matcher05.find()) {
val strBuffer = matcher05.group()
val pattern06 = Pattern.compile("[0-9]+")
val matcher06 = pattern06.matcher(strBuffer)
if (matcher06.find()) {
val strInterval = matcher06.group()
interval = strInterval.toInt()
intervalFlag = true
}
}
val pattern07 = Pattern.compile("FREQ=DAILY")
val matcher07 = pattern07.matcher(it)
if (matcher07.find()) {
daily = true
}
val pattern08 = Pattern.compile("FREQ=WEEKLY")
val matcher08 = pattern08.matcher(it)
if (matcher08.find()) {
weekly = true
}
val pattern09 = Pattern.compile("FREQ=MONTHLY")
val matcher09 = pattern09.matcher(it)
if (matcher09.find()) {
monthly = true
}
val pattern10 = Pattern.compile("FREQ=YEARLY")
val matcher10 = pattern10.matcher(it)
if (matcher10.find()) {
yearly = true
}
val calEnd = calStart
if (countFlag && intervalFlag) {
if (daily) {
calEnd.add(Calendar.DATE, count*interval)
}
if (weekly) {
calEnd.add(Calendar.WEEK_OF_YEAR, count*interval)
}
if (monthly) {
calEnd.add(Calendar.MONTH, count*interval)
}
if (yearly) {
calEnd.add(Calendar.YEAR, count*interval)
}
val year=String.format("%04d", calEnd.get(Calendar.YEAR))
val month=String.format("%02d", calEnd.get(Calendar.MONTH)+1)
val date=String.format("%02d", calEnd.get(Calendar.DATE))
resultEndDate = "$year-$month-$date"
}

return resultEndDate
}
}

アプリをリリースするわけですが、その時につまづきました。

Google Play アプリ署名を有効にして、Android App Bundleとしてリリースしました。

この時に、API Consoleで、SHA1認証キーを入力するわけですが、

f:id:uchida001tmhr:20181021143033p:plain

名証明書のフィンガープリントには、Google Developer Consoleの「アプリのリリース」→「アプリの署名」の「アプリへの署名証明書」のSHA1フィンガープリントを持ってくる必要があります。keytoolで出力したフィンガープリントではうまくいきませんので、ご注意を!!