|
| 1 | +/* |
| 2 | + * Copyright 2019-2026, Tomasz Żebrowski |
| 3 | + * |
| 4 | + * <p>Licensed to the Apache Software Foundation (ASF) under one or more contributor license |
| 5 | + * agreements. See the NOTICE file distributed with this work for additional information regarding |
| 6 | + * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance with the License. You may obtain a |
| 8 | + * copy of the License at |
| 9 | + * |
| 10 | + * <p>http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * <p>Unless required by applicable law or agreed to in writing, software distributed under the |
| 13 | + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 14 | + * express or implied. See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.obd.graphs.preferences |
| 18 | + |
| 19 | +import android.content.Context |
| 20 | +import android.util.AttributeSet |
| 21 | +import android.util.Log |
| 22 | +import androidx.appcompat.app.AlertDialog |
| 23 | +import androidx.preference.SwitchPreferenceCompat |
| 24 | +import org.obd.graphs.GOOGLE_SIGN_IN_REQUEST |
| 25 | +import org.obd.graphs.R |
| 26 | +import org.obd.graphs.sendBroadcastEvent |
| 27 | + |
| 28 | +private const val LOG_TAG = "DriveAutoSyncEnablerCheckBoxPreference" |
| 29 | + |
| 30 | +class DriveAutoSyncEnablerCheckBoxPreference( |
| 31 | + context: Context, |
| 32 | + attrs: AttributeSet? |
| 33 | +) : SwitchPreferenceCompat(context, attrs) { |
| 34 | + |
| 35 | + init { |
| 36 | + setOnPreferenceChangeListener { _, newValue -> |
| 37 | + val isEnabling = newValue as Boolean |
| 38 | + if (isEnabling) { |
| 39 | + AlertDialog |
| 40 | + .Builder(context) |
| 41 | + .setTitle(context.getString(R.string.pref_trips_drive_auto_sync_enabled_sign_in_dialog_title)) |
| 42 | + .setMessage(context.getString(R.string.pref_trips_drive_auto_sync_enabled_sign_in_dialog_summary)) |
| 43 | + .setCancelable(false) |
| 44 | + .setPositiveButton(context.getString(R.string.dialog_ask_question_yes)) { _, _ -> |
| 45 | + sendBroadcastEvent(GOOGLE_SIGN_IN_REQUEST) |
| 46 | + isChecked = true |
| 47 | + }.setNegativeButton(context.getString(R.string.dialog_ask_question_no)) { dialog, _ -> |
| 48 | + Log.i(LOG_TAG, "Disabling google drive auto sync") |
| 49 | + dialog.dismiss() |
| 50 | + }.show() |
| 51 | + false |
| 52 | + } else { |
| 53 | + true |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | +} |
0 commit comments