Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dgudim committed Nov 1, 2022
1 parent 6ba64b9 commit df5f9ea
Show file tree
Hide file tree
Showing 40 changed files with 760 additions and 791 deletions.
24 changes: 12 additions & 12 deletions app/src/main/java/prototype/xd/scheduler/HomeFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import static prototype.xd.scheduler.utilities.Keys.DAY_FLAG_GLOBAL_STR;
import static prototype.xd.scheduler.utilities.Keys.IS_COMPLETED;
import static prototype.xd.scheduler.utilities.Keys.TEXT_VALUE;
import static prototype.xd.scheduler.utilities.PreferencesStore.preferences_service;
import static prototype.xd.scheduler.utilities.PreferencesStore.servicePreferences;

import android.os.Bundle;
import android.view.LayoutInflater;
Expand All @@ -25,7 +25,7 @@

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import prototype.xd.scheduler.entities.Group;
Expand Down Expand Up @@ -59,32 +59,32 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
todoListEntryStorage.getTodoListViewAdapter().setDateUpdateListener(calendarView::notifyCurrentDayChanged);

long day;
if ((day = preferences_service.getLong(Keys.PREVIOUSLY_SELECTED_DAY, 0)) != 0) {
if ((day = servicePreferences.getLong(Keys.PREVIOUSLY_SELECTED_DAY, 0)) != 0) {
calendarView.selectDay(day);
} else {
calendarView.selectDate(DateManager.currentDate);
}

TextView statusText = view.findViewById(R.id.status_text);
calendarView.setOnDateChangeListener((selectedDate, context) -> {
long epoch_day = selectedDate.toEpochDay();
preferences_service.edit().putLong(Keys.PREVIOUSLY_SELECTED_DAY, epoch_day).apply();
updateDate(epoch_day, true);
long epochDay = selectedDate.toEpochDay();
servicePreferences.edit().putLong(Keys.PREVIOUSLY_SELECTED_DAY, epochDay).apply();
updateDate(epochDay, true);
todoListEntryStorage.updateTodoListAdapter(false, false);
updateStatusText(statusText);
});

calendarView.setOnMonthPreChangeListener((calendarMonth, first_visible_day, last_visible_day, context) ->
calendarView.setOnMonthPreChangeListener((calendarMonth, firstVisibleDay, lastVisibleDay, context) ->
// load current month before displaying the data
todoListEntryStorage.lazyLoadEntries(context, first_visible_day, last_visible_day));
todoListEntryStorage.lazyLoadEntries(context, firstVisibleDay, lastVisibleDay));

view.findViewById(R.id.to_current_date_button).setOnClickListener(v -> {
calendarView.selectDay(currentDay);
preferences_service.edit().remove(Keys.PREVIOUSLY_SELECTED_DAY).apply();
servicePreferences.edit().remove(Keys.PREVIOUSLY_SELECTED_DAY).apply();
});

view.<FloatingActionButton>findViewById(R.id.fab).setOnClickListener(view1 -> {
final ArrayList<Group> groupList = todoListEntryStorage.getGroups();
final List<Group> groupList = todoListEntryStorage.getGroups();
displayEditTextSpinnerDialogue(view1.getContext(), R.string.add_event_fab, -1, R.string.event_name_input_hint,
R.string.cancel, R.string.add, R.string.add_to_global_list, "", groupList, 0,
(view2, text, selectedIndex) -> {
Expand Down Expand Up @@ -127,7 +127,7 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
updateDate(DAY_FLAG_GLOBAL, true);

long day;
if ((day = preferences_service.getLong(Keys.PREVIOUSLY_SELECTED_DAY, 0)) != 0) {
if ((day = servicePreferences.getLong(Keys.PREVIOUSLY_SELECTED_DAY, 0)) != 0) {
currentlySelectedDay = day;
}

Expand All @@ -142,7 +142,7 @@ private void updateStatusText(TextView statusText) {
@Override
public void onDestroy() {
todoListEntryStorage = null;
preferences_service.edit().remove(Keys.PREVIOUSLY_SELECTED_DAY).apply();
servicePreferences.edit().remove(Keys.PREVIOUSLY_SELECTED_DAY).apply();
super.onDestroy();
}
}
59 changes: 25 additions & 34 deletions app/src/main/java/prototype/xd/scheduler/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import static android.util.Log.ERROR;
import static android.util.Log.INFO;
import static prototype.xd.scheduler.utilities.BitmapUtilities.fingerPrintAndSaveBitmap;
import static prototype.xd.scheduler.utilities.DateManager.availableDays;
import static prototype.xd.scheduler.utilities.DateManager.AVAILABLE_DAYS;
import static prototype.xd.scheduler.utilities.DateManager.getCurrentTimestamp;
import static prototype.xd.scheduler.utilities.Keys.ROOT_DIR;
import static prototype.xd.scheduler.utilities.Logger.log;
import static prototype.xd.scheduler.utilities.Logger.logException;
import static prototype.xd.scheduler.utilities.PreferencesStore.preferences;
import static prototype.xd.scheduler.utilities.PreferencesStore.preferences_service;
import static prototype.xd.scheduler.utilities.PreferencesStore.servicePreferences;
import static prototype.xd.scheduler.utilities.Utilities.getRootDir;

import android.Manifest;
Expand Down Expand Up @@ -78,14 +78,6 @@ protected void onCreate(Bundle savedInstanceState) {
}
}

/* try {
Class.forName("dalvik.system.CloseGuard")
.getMethod("setEnabled", boolean.class)
.invoke(null, true);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}*/

// init theme
AppCompatDelegate.setDefaultNightMode(preferences.getInt(Keys.APP_THEME, Keys.DEFAULT_APP_THEME));

Expand All @@ -104,17 +96,16 @@ protected void onCreate(Bundle savedInstanceState) {
if (!refreshPermissionStates(false)) {
setContentView(R.layout.permissions_request_screen);
refreshPermissionStates(true);
View grant_button = findViewById(R.id.grant_permissions_button);
grant_button.setOnClickListener(v -> requestPermissions(PERMISSIONS, REQUEST_CODE_PERMISSIONS));
findViewById(R.id.grant_permissions_button).setOnClickListener(v -> requestPermissions(PERMISSIONS, REQUEST_CODE_PERMISSIONS));
} else {
if (isServiceBeingKilled()) {
setContentView(R.layout.service_keep_alive_screen);

if (preferences_service.getInt(Keys.SERVICE_KILLED_IGNORE_BUTTON_CLICKED, 0) > 1) {
if (servicePreferences.getInt(Keys.SERVICE_KILLED_IGNORE_BUTTON_CLICKED, 0) > 1) {
View dontBotherButton = findViewById(R.id.never_ask_again_button);
dontBotherButton.setVisibility(View.VISIBLE);
dontBotherButton.setOnClickListener(v -> {
preferences_service.edit().putBoolean(Keys.SERVICE_KILLED_DONT_BOTHER, true).apply();
servicePreferences.edit().putBoolean(Keys.SERVICE_KILLED_DONT_BOTHER, true).apply();
launchMainActivity();
});
}
Expand All @@ -123,15 +114,15 @@ protected void onCreate(Bundle savedInstanceState) {
startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("https://dontkillmyapp.com/"))));

findViewById(R.id.ignore_button).setOnClickListener(v -> {
preferences_service.edit().putInt(Keys.SERVICE_KILL_THRESHOLD_REACHED, 0).apply();
preferences_service.edit().putInt(Keys.SERVICE_KILLED_IGNORE_BUTTON_CLICKED,
preferences_service.getInt(Keys.SERVICE_KILLED_IGNORE_BUTTON_CLICKED, 0) + 1).apply();
servicePreferences.edit().putInt(Keys.SERVICE_KILL_THRESHOLD_REACHED, 0).apply();
servicePreferences.edit().putInt(Keys.SERVICE_KILLED_IGNORE_BUTTON_CLICKED,
servicePreferences.getInt(Keys.SERVICE_KILLED_IGNORE_BUTTON_CLICKED, 0) + 1).apply();
launchMainActivity();
});
} else if (isCrapPhone()) {
setContentView(R.layout.you_have_a_xiaomi_phone_screen);
findViewById(R.id.ignore_button).setOnClickListener(v -> {
preferences_service.edit().putBoolean(Keys.XIAOMI_MESSAGE_IGNORE, true).apply();
servicePreferences.edit().putBoolean(Keys.XIAOMI_MESSAGE_IGNORE, true).apply();
launchMainActivity();
});

Expand All @@ -142,15 +133,15 @@ protected void onCreate(Bundle savedInstanceState) {
}

private boolean isServiceBeingKilled() {
if (preferences_service.getBoolean(Keys.SERVICE_KILLED_DONT_BOTHER, false)) {
if (servicePreferences.getBoolean(Keys.SERVICE_KILLED_DONT_BOTHER, false)) {
return false;
}
long prevTime = preferences_service.getLong(Keys.LAST_UPDATE_TIME, getCurrentTimestamp());
long prevTime = servicePreferences.getLong(Keys.LAST_UPDATE_TIME, getCurrentTimestamp());
if (getCurrentTimestamp() - prevTime > 4 * 60 * 60 * 1000) {
preferences_service.edit().putInt(Keys.SERVICE_KILL_THRESHOLD_REACHED,
preferences_service.getInt(Keys.SERVICE_KILL_THRESHOLD_REACHED, 0) + 1).apply();
servicePreferences.edit().putInt(Keys.SERVICE_KILL_THRESHOLD_REACHED,
servicePreferences.getInt(Keys.SERVICE_KILL_THRESHOLD_REACHED, 0) + 1).apply();
}
return preferences_service.getInt(Keys.SERVICE_KILL_THRESHOLD_REACHED, 0) > 15;
return servicePreferences.getInt(Keys.SERVICE_KILL_THRESHOLD_REACHED, 0) > 15;
}

private void launchMainActivity() {
Expand All @@ -159,27 +150,27 @@ private void launchMainActivity() {
}

private boolean refreshPermissionStates(boolean display) {
boolean storage_granted = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
boolean calendar_granted = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) == PackageManager.PERMISSION_GRANTED;
boolean storageGranted = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
boolean calendarGranted = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) == PackageManager.PERMISSION_GRANTED;

if (display) {
setPermissionChipColor(calendar_granted,
setPermissionChipColor(calendarGranted,
findViewById(R.id.calendar_permission_granted),
findViewById(R.id.calendar_permission_granted_bg));

setPermissionChipColor(storage_granted,
setPermissionChipColor(storageGranted,
findViewById(R.id.storage_permission_granted),
findViewById(R.id.storage_permission_granted_bg));
}

return storage_granted && calendar_granted;
return storageGranted && calendarGranted;
}

private void setPermissionChipColor(boolean permission_granted, TextView permission_text, CardView permission_text_bg) {
permission_text.setText(permission_granted ? R.string.permissions_granted : R.string.permissions_not_granted);
permission_text.setTextColor(MaterialColors.getColor(permission_text, permission_granted ? R.attr.colorOnTertiaryContainer : R.attr.colorOnErrorContainer,
permission_granted ? Color.GREEN : Color.RED));
permission_text_bg.setCardBackgroundColor(MaterialColors.getColor(permission_text_bg, permission_granted ? R.attr.colorTertiaryContainer : R.attr.colorErrorContainer,
private void setPermissionChipColor(boolean permissionGranted, TextView permissionText, CardView permissionTextBg) {
permissionText.setText(permissionGranted ? R.string.permissions_granted : R.string.permissions_not_granted);
permissionText.setTextColor(MaterialColors.getColor(permissionText, permissionGranted ? R.attr.colorOnTertiaryContainer : R.attr.colorOnErrorContainer,
permissionGranted ? Color.GREEN : Color.RED));
permissionTextBg.setCardBackgroundColor(MaterialColors.getColor(permissionTextBg, permissionGranted ? R.attr.colorTertiaryContainer : R.attr.colorErrorContainer,
Color.LTGRAY));
}

Expand All @@ -204,7 +195,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent intent)
InputStream stream = getContentResolver().openInputStream(uri);
if (stream != null) {
fingerPrintAndSaveBitmap(BitmapFactory.decodeStream(stream),
new File(getRootDir(), availableDays[requestCode] + ".png"));
new File(getRootDir(), AVAILABLE_DAYS[requestCode] + ".png"));
stream.close();
} else {
log(ERROR, NAME, "stream null for uri: " + uri.getPath());
Expand Down
69 changes: 34 additions & 35 deletions app/src/main/java/prototype/xd/scheduler/SettingsFragment.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package prototype.xd.scheduler;

import static prototype.xd.scheduler.utilities.Keys.SERVICE_UPDATE_SIGNAL;
import static prototype.xd.scheduler.utilities.PreferencesStore.preferences_service;
import static prototype.xd.scheduler.utilities.PreferencesStore.servicePreferences;
import static prototype.xd.scheduler.utilities.SystemCalendarUtils.getAllCalendars;

import android.os.Bundle;
Expand All @@ -14,19 +14,20 @@
import androidx.fragment.app.Fragment;

import java.util.ArrayList;
import java.util.List;

import prototype.xd.scheduler.adapters.SettingsListViewAdapter;
import prototype.xd.scheduler.entities.calendars.SystemCalendar;
import prototype.xd.scheduler.entities.settingsEntries.AdaptiveBackgroundSettingsEntry;
import prototype.xd.scheduler.entities.settingsEntries.AppThemeSelectorEntry;
import prototype.xd.scheduler.entities.settingsEntries.CalendarAccountSettingsEntry;
import prototype.xd.scheduler.entities.settingsEntries.CalendarSettingsEntry;
import prototype.xd.scheduler.entities.settingsEntries.CompoundCustomizationEntry;
import prototype.xd.scheduler.entities.settingsEntries.DiscreteSeekBarSettingsEntry;
import prototype.xd.scheduler.entities.settingsEntries.ResetButtonSettingsEntry;
import prototype.xd.scheduler.entities.settingsEntries.SettingsEntry;
import prototype.xd.scheduler.entities.settingsEntries.SwitchSettingsEntry;
import prototype.xd.scheduler.entities.settingsEntries.TitleBarSettingsEntry;
import prototype.xd.scheduler.entities.settings_entries.AdaptiveBackgroundSettingsEntry;
import prototype.xd.scheduler.entities.settings_entries.AppThemeSelectorEntry;
import prototype.xd.scheduler.entities.settings_entries.CalendarAccountSettingsEntry;
import prototype.xd.scheduler.entities.settings_entries.CalendarSettingsEntry;
import prototype.xd.scheduler.entities.settings_entries.CompoundCustomizationEntry;
import prototype.xd.scheduler.entities.settings_entries.DiscreteSeekBarSettingsEntry;
import prototype.xd.scheduler.entities.settings_entries.ResetButtonSettingsEntry;
import prototype.xd.scheduler.entities.settings_entries.SettingsEntry;
import prototype.xd.scheduler.entities.settings_entries.SwitchSettingsEntry;
import prototype.xd.scheduler.entities.settings_entries.TitleBarSettingsEntry;
import prototype.xd.scheduler.utilities.Keys;
import prototype.xd.scheduler.views.settings.SystemCalendarSettings;

Expand All @@ -43,7 +44,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa

@Override
public void onDestroyView() {
preferences_service.edit().putBoolean(SERVICE_UPDATE_SIGNAL, true).apply();
servicePreferences.edit().putBoolean(SERVICE_UPDATE_SIGNAL, true).apply();
super.onDestroyView();
}

Expand All @@ -58,10 +59,11 @@ public void notifyBgSelected() {
adaptiveBackgroundSettingsEntry.notifyBackgroundUpdated();
}

@Override
public void onViewCreated(@NonNull final View view, final Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ArrayList<SettingsEntry> settingsEntries = new ArrayList<>();

List<SettingsEntry> settingsEntries = new ArrayList<>();

adaptiveBackgroundSettingsEntry = new AdaptiveBackgroundSettingsEntry(this);

Expand Down Expand Up @@ -98,38 +100,35 @@ public void onViewCreated(@NonNull final View view, final Bundle savedInstanceSt
SettingsListViewAdapter settingsListViewAdapter = new SettingsListViewAdapter(settingsEntries);
((ListView) view.findViewById(R.id.settingsList)).setAdapter(settingsListViewAdapter);

ArrayList<SettingsEntry> settingsEntries_additional = new ArrayList<>();
List<SettingsEntry> additionalSettingsEntries = new ArrayList<>();
new Thread(() -> {
settingsEntries_additional.add(new TitleBarSettingsEntry(getString(R.string.category_system_calendars)));
ArrayList<SystemCalendar> calendars = getAllCalendars(view.getContext(), true);
ArrayList<ArrayList<SystemCalendar>> calendars_sorted = new ArrayList<>();
ArrayList<String> calendars_sorted_names = new ArrayList<>();
additionalSettingsEntries.add(new TitleBarSettingsEntry(getString(R.string.category_system_calendars)));
List<SystemCalendar> calendars = getAllCalendars(view.getContext(), true);
List<List<SystemCalendar>> sortedCalendars = new ArrayList<>();
List<String> sortedCalendarNames = new ArrayList<>();

for (int i = 0; i < calendars.size(); i++) {
SystemCalendar calendar = calendars.get(i);
if (calendars_sorted_names.contains(calendar.account_name)) {
calendars_sorted.get(calendars_sorted_names.indexOf(calendar.account_name)).add(calendar);
for (SystemCalendar calendar: calendars) {
if (sortedCalendarNames.contains(calendar.account_name)) {
sortedCalendars.get(sortedCalendarNames.indexOf(calendar.account_name)).add(calendar);
} else {
ArrayList<SystemCalendar> calendar_group = new ArrayList<>();
calendar_group.add(calendar);
calendars_sorted.add(calendar_group);
calendars_sorted_names.add(calendar.account_name);
List<SystemCalendar> calendarGroup = new ArrayList<>();
calendarGroup.add(calendar);
sortedCalendars.add(calendarGroup);
sortedCalendarNames.add(calendar.account_name);
}
}

for (int g = 0; g < calendars_sorted.size(); g++) {
ArrayList<SystemCalendar> calendar_group = calendars_sorted.get(g);
SystemCalendar calendar0 = calendar_group.get(0);
for (List<SystemCalendar> calendarGroup: sortedCalendars) {
SystemCalendar calendar0 = calendarGroup.get(0);

settingsEntries_additional.add(new CalendarAccountSettingsEntry(SettingsFragment.this, calendar0));
additionalSettingsEntries.add(new CalendarAccountSettingsEntry(SettingsFragment.this, calendar0));

for (int c = 0; c < calendar_group.size(); c++) {
SystemCalendar current_calendar = calendar_group.get(c);
settingsEntries_additional.add(new CalendarSettingsEntry(SettingsFragment.this, current_calendar));
for (SystemCalendar currentCalendar: calendarGroup) {
additionalSettingsEntries.add(new CalendarSettingsEntry(SettingsFragment.this, currentCalendar));
}
}
requireActivity().runOnUiThread(() -> {
settingsEntries.addAll(settingsEntries_additional);
settingsEntries.addAll(additionalSettingsEntries);
settingsListViewAdapter.notifyDataSetChanged();
});
}, "SSCfetch thread").start();
Expand Down
Loading

0 comments on commit df5f9ea

Please sign in to comment.