Skip to content

Commit

Permalink
added option and prompt to opt out/in of Firebase analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
sds100 committed Mar 2, 2019
1 parent f3ee68a commit d1b34d2
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ import android.view.View
import androidx.annotation.ColorRes
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.edit
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.gson.Gson
import io.github.sds100.keymapper.BuildConfig
import io.github.sds100.keymapper.KeyMap
Expand All @@ -34,10 +36,7 @@ import io.github.sds100.keymapper.service.MyIMEService
import io.github.sds100.keymapper.util.*
import kotlinx.android.synthetic.main.activity_home.*
import kotlinx.android.synthetic.main.content_home.*
import org.jetbrains.anko.append
import org.jetbrains.anko.defaultSharedPreferences
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
import org.jetbrains.anko.*

class HomeActivity : AppCompatActivity(), SelectionCallback,
OnItemClickListener<KeymapAdapterModel>, MenuItem.OnMenuItemClickListener {
Expand Down Expand Up @@ -107,6 +106,34 @@ class HomeActivity : AppCompatActivity(), SelectionCallback,
val intentFilter = IntentFilter()
intentFilter.addAction(Intent.ACTION_INPUT_METHOD_CHANGED)
registerReceiver(mBroadcastReceiver, intentFilter)

//ask the user whether they want to enable analytics
val isFirstTime = defaultSharedPreferences.getBoolean(
str(R.string.key_pref_first_time), true
)

defaultSharedPreferences.edit {
if (isFirstTime) {
alert {
titleResource = R.string.title_pref_data_collection
messageResource = R.string.summary_pref_data_collection
positiveButton(R.string.pos_opt_in) {
putBoolean(str(R.string.key_pref_data_collection), true).commit()
setFirebaseDataCollection()
putBoolean(str(R.string.key_pref_first_time), false).commit()
}

negativeButton(R.string.neg_opt_out) {
putBoolean(str(R.string.key_pref_data_collection), false).commit()
setFirebaseDataCollection()
putBoolean(str(R.string.key_pref_first_time), false).commit()
}
}.show()

} else {
setFirebaseDataCollection()
}
}
}

override fun onMenuItemClick(item: MenuItem?): Boolean {
Expand Down Expand Up @@ -316,4 +343,12 @@ class HomeActivity : AppCompatActivity(), SelectionCallback,
private fun setStatusBarColor(@ColorRes colorId: Int) {
window.statusBarColor = color(colorId)
}

private fun setFirebaseDataCollection() {
val isDataCollectionEnabled = defaultSharedPreferences.getBoolean(
str(R.string.key_pref_data_collection),
bool(R.bool.default_value_data_collection))

FirebaseAnalytics.getInstance(this@HomeActivity).setAnalyticsCollectionEnabled(isDataCollectionEnabled)
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values/bools.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
<bool name="default_value_show_volume_dialog_on_adjust">true</bool>
<bool name="default_value_auto_change_ime_on_connection">false</bool>
<bool name="default_value_allow_root_features">false</bool>
<bool name="default_value_data_collection">false</bool>
</resources>
8 changes: 8 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@
<!-- Dialog buttons -->
<string name="pos_yes">Yes</string>
<string name="pos_done">Done</string>
<string name="pos_opt_in">Opt in</string>

<string name="neg_opt_out">Opt out</string>
<string name="neg_no">No</string>
<string name="neg_cancel">Cancel</string>
<!-- Dialog buttons -->
Expand All @@ -116,10 +118,16 @@

<!--preferences-->
<!-- Look in the bools.xml file to find their default values -->
<string name="key_pref_first_time">pref_first_time</string>

<string name="title_pref_show_notification">Show Notification</string>
<string name="key_pref_show_notification">pref_show_notification</string>
<string name="summary_pref_show_notification">Show a persistent notification to allow you to pick an input method. (The app requires ROOT permission for it to work on Android 8.1 and higher).</string>

<string name="title_pref_data_collection">Anonymous analytics</string>
<string name="key_pref_data_collection">pref_data_collection</string>
<string name="summary_pref_data_collection">Do you want to help improve Key Mapper by allowing it to anonymously collect data about how you use it such as crash reports? The data is collected by Google Firebase. The privacy policy is located in the About activity.</string>

<string name="key_pref_bluetooth_devices">pref_bluetooth_devices</string>
<string name="title_pref_bluetooth_devices">Choose Bluetooth Devices…</string>

Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/xml-v27/preferences.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<?xml version="1.0" encoding="utf-8"?><!--Android 8.1+ is much more strict so a lot of stuff only works on rooted devices. :( -->
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.preference.SwitchPreference
android:defaultValue="@bool/default_value_data_collection"
android:key="@string/key_pref_data_collection"
android:summary="@string/summary_pref_data_collection"
android:title="@string/title_pref_data_collection" />

<androidx.preference.SwitchPreference
android:defaultValue="@bool/default_value_show_toast"
android:key="@string/key_pref_show_toast_when_action_performed"
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.preference.SwitchPreference
android:defaultValue="@bool/default_value_data_collection"
android:key="@string/key_pref_data_collection"
android:summary="@string/summary_pref_data_collection"
android:title="@string/title_pref_data_collection" />

<androidx.preference.SwitchPreference
android:defaultValue="@bool/default_value_show_toast"
android:key="@string/key_pref_show_toast_when_action_performed"
Expand Down

0 comments on commit d1b34d2

Please sign in to comment.