Move settings to menu

This commit is contained in:
xynngh 2020-05-06 17:24:32 +04:00
parent 7ad9cecc73
commit 731c35434e
5 changed files with 62 additions and 66 deletions

View File

@ -14,7 +14,6 @@ import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;
import androidx.recyclerview.widget.RecyclerView;
import org.greenrobot.eventbus.EventBus;
@ -36,6 +35,8 @@ public class MainActivity extends AppCompatActivity {
private final Settings settings = App.getSettings();
private final UpdateScheduler updateScheduler = UpdateScheduler.get(App.getInstance());
private CallLogItemRecyclerViewAdapter callLogAdapter;
private List<CallLogItem> callLogItems = new ArrayList<>();
@ -50,28 +51,6 @@ public class MainActivity extends AppCompatActivity {
callLogAdapter = new CallLogItemRecyclerViewAdapter(callLogItems, this::onCallLogItemClicked);
RecyclerView recyclerView = findViewById(R.id.callLogList);
recyclerView.setAdapter(callLogAdapter);
SwitchCompat notificationsSwitch = findViewById(R.id.notificationsEnabledSwitch);
notificationsSwitch.setChecked(settings.getIncomingCallNotifications());
notificationsSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
settings.setIncomingCallNotifications(isChecked);
checkPermissions();
});
SwitchCompat blockCallsSwitch = findViewById(R.id.blockCallsSwitch);
blockCallsSwitch.setChecked(settings.getBlockCalls());
blockCallsSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
settings.setBlockCalls(isChecked);
checkPermissions();
});
UpdateScheduler updateScheduler = UpdateScheduler.get(this);
SwitchCompat autoUpdateSwitch = findViewById(R.id.autoUpdateEnabledSwitch);
autoUpdateSwitch.setChecked(updateScheduler.isAutoUpdateScheduled());
autoUpdateSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked) updateScheduler.scheduleAutoUpdates();
else updateScheduler.cancelAutoUpdateWorker();
});
}
@Override
@ -80,6 +59,20 @@ public class MainActivity extends AppCompatActivity {
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.menu_show_notifications).setChecked(
settings.getIncomingCallNotifications());
menu.findItem(R.id.menu_block_calls).setChecked(
settings.getBlockCalls());
menu.findItem(R.id.menu_auto_updates).setChecked(
updateScheduler.isAutoUpdateScheduled());
return super.onPrepareOptionsMenu(menu);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
@ -175,6 +168,21 @@ public class MainActivity extends AppCompatActivity {
TaskService.start(this, TaskService.TASK_DOWNLOAD_MAIN_DB);
}
public void onShowNotificationsChanged(MenuItem item) {
settings.setIncomingCallNotifications(!item.isChecked());
checkPermissions();
}
public void onBlockCallsChanged(MenuItem item) {
settings.setBlockCalls(!item.isChecked());
checkPermissions();
}
public void onAutoUpdatesChanged(MenuItem item) {
if (!item.isChecked()) updateScheduler.scheduleAutoUpdates();
else updateScheduler.cancelAutoUpdateWorker();
}
public void onOpenDebugActivity(MenuItem item) {
startActivity(new Intent(this, DebugActivity.class));
}
@ -218,7 +226,7 @@ public class MainActivity extends AppCompatActivity {
= new AsyncTask<Void, Void, List<CallLogItem>>() {
@Override
protected List<CallLogItem> doInBackground(Void... voids) {
List<CallLogItem> items = CallLogHelper.getRecentCalls(MainActivity.this, 10);
List<CallLogItem> items = CallLogHelper.getRecentCalls(MainActivity.this, 20);
for (CallLogItem item : items) {
item.numberInfo = DatabaseSingleton.getCommunityDatabase().isOperational()
@ -249,6 +257,9 @@ public class MainActivity extends AppCompatActivity {
}
private void setCallLogVisibility(boolean visible) {
findViewById(R.id.callLogPermissionMessage)
.setVisibility(visible ? View.GONE : View.VISIBLE);
int visibility = visible ? View.VISIBLE : View.GONE;
findViewById(R.id.callLogTitle).setVisibility(visibility);
findViewById(R.id.callLogList).setVisibility(visibility);

View File

@ -8,48 +8,16 @@
tools:context=".MainActivity">
<TextView
android:id="@+id/callLogPermissionMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_marginStart="@dimen/text_margin"
android:layout_marginLeft="@dimen/text_margin"
android:paddingTop="@dimen/item_padding"
android:text="@string/general_settings"
android:textAlignment="textStart" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="?android:listDivider"
android:orientation="vertical"
android:paddingLeft="@dimen/item_padding"
android:paddingRight="@dimen/item_padding"
android:showDividers="middle">
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/notificationsEnabledSwitch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/text_margin"
android:layout_marginBottom="@dimen/text_margin"
android:text="@string/incoming_call_notifications_enabled" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/blockCallsSwitch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/text_margin"
android:layout_marginBottom="@dimen/text_margin"
android:text="@string/block_calls" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/autoUpdateEnabledSwitch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/text_margin"
android:layout_marginBottom="@dimen/text_margin"
android:text="@string/auto_update_enabled" />
</LinearLayout>
android:text="@string/call_log_permission_message"
android:textAlignment="textStart"
android:visibility="gone" />
<TextView
android:id="@+id/callLogTitle"

View File

@ -1,6 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_show_notifications"
android:checkable="true"
android:onClick="onShowNotificationsChanged"
android:title="@string/incoming_call_notifications" />
<item
android:id="@+id/menu_block_calls"
android:checkable="true"
android:onClick="onBlockCallsChanged"
android:title="@string/block_calls" />
<item
android:id="@+id/menu_auto_updates"
android:checkable="true"
android:onClick="onAutoUpdatesChanged"
android:title="@string/auto_updates" />
<item
android:onClick="onOpenDebugActivity"
android:title="@string/open_debug_activity" />

View File

@ -21,7 +21,7 @@
<string name="sia_category_robocall">Робот/автомат</string>
<string name="sia_category_nonprofit">Некоммерческая орг</string>
<string name="block_calls">Блокировать нежелательные вызовы</string>
<string name="auto_update_enabled">Авто-обновление баз номеров</string>
<string name="auto_updates">Авто-обновление баз номеров</string>
<string name="open_debug_activity">Открыть экран отладки</string>
<string name="debug_activity_label">Отладка</string>
<string name="debug_query_db">Искать в общей базе</string>
@ -30,10 +30,9 @@
<string name="debug_update_db">Обновить базы</string>
<string name="debug_not_found">Не найдено</string>
<string name="debug_update_result">Обновление завершено. Версия: %d</string>
<string name="incoming_call_notifications_enabled">Отображать уведомление при входящих</string>
<string name="incoming_call_notifications">Отображать уведомление при входящих</string>
<string name="title_activity_reviews">Отзывы</string>
<string name="reviews_loading">Загружаем отзывы…</string>
<string name="general_settings">Основные настройки</string>
<string name="sia_category_fax">Факс</string>
<string name="sia_category_nuisance">Злонамеренный</string>
<string name="sia_category_unsolicited">Нежелательный</string>

View File

@ -54,7 +54,7 @@
<string name="denied_permissions_message_info">Notification functionality may not work due to denied permissions</string>
<string name="denied_permissions_message_blocking">Blocking functionality may not work due to denied permissions</string>
<string name="general_settings">General settings</string>
<string name="call_log_permission_message">Grant \"Phone\" permission to see recent calls</string>
<string name="no_main_db_title">Download main database</string>
<string name="no_main_db_text">Database is not present! For the app to perform most of its functions you need to download DB. It will take around 25 MB of traffic.</string>
@ -68,9 +68,9 @@
<string name="back">Back</string>
<string name="no">No</string>
<string name="incoming_call_notifications_enabled">Incoming call notifications enabled</string>
<string name="incoming_call_notifications">Incoming call notifications</string>
<string name="block_calls">Block unwanted calls</string>
<string name="auto_update_enabled">Auto-update enabled</string>
<string name="auto_updates">Auto-update database</string>
<string name="open_debug_activity">Open debug screen</string>
<string name="debug_activity_label">Debug</string>