forked from criticalmaps/criticalmaps-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsFragment.java
More file actions
195 lines (156 loc) · 7.32 KB
/
SettingsFragment.java
File metadata and controls
195 lines (156 loc) · 7.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package de.stephanlindauer.criticalmaps.fragments;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.OpenableColumns;
import android.text.format.Formatter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import javax.inject.Inject;
import de.stephanlindauer.criticalmaps.App;
import de.stephanlindauer.criticalmaps.R;
import de.stephanlindauer.criticalmaps.databinding.FragmentSettingsBinding;
import de.stephanlindauer.criticalmaps.handler.ChooseGpxFileHandler;
import de.stephanlindauer.criticalmaps.prefs.SharedPrefsKeys;
import de.stephanlindauer.criticalmaps.model.StorageLocation;
import de.stephanlindauer.criticalmaps.vo.RequestCodes;
import info.metadude.android.typedpreferences.BooleanPreference;
import info.metadude.android.typedpreferences.StringPreference;
import timber.log.Timber;
import static de.stephanlindauer.criticalmaps.utils.GpxUtils.persistPermissionOnFile;
import org.maplibre.android.offline.OfflineManager;
public class SettingsFragment extends Fragment {
@Inject
SharedPreferences sharedPreferences;
private FragmentSettingsBinding binding;
private StorageLocation storageLocation;
@Inject
App app;
@Override
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
binding = FragmentSettingsBinding.inflate(inflater, container, false);
return binding.getRoot();
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
App.components().inject(this);
storageLocation = new StorageLocation(app.getFilesDir());
}
@Override
public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
updateClearCachePref();
updateStorageGraph();
updateGpxFileName();
binding.settingsShowOnLockscreenCheckbox.setChecked(
new BooleanPreference(sharedPreferences, SharedPrefsKeys.SHOW_ON_LOCKSCREEN).get());
binding.settingsKeepScreenOnCheckbox.setChecked(
new BooleanPreference(sharedPreferences, SharedPrefsKeys.KEEP_SCREEN_ON).get());
binding.settingsShowGpxCheckbox.setChecked(
new BooleanPreference(sharedPreferences, SharedPrefsKeys.SHOW_GPX).get());
binding.settingsClearCacheButton.setOnClickListener(v -> handleClearCacheClicked());
binding.settingsShowOnLockscreenCheckbox.setOnCheckedChangeListener(
(buttonView, isChecked) -> handleShowOnLockscreenChecked(isChecked));
binding.settingsKeepScreenOnCheckbox.setOnCheckedChangeListener(
(buttonView, isChecked) -> handleKeepScreenOnChecked(isChecked));
binding.settingsShowGpxCheckbox.setOnCheckedChangeListener(
(buttonView, isChecked) -> handleShowTrack(isChecked));
binding.settingsChooseGpxContainer.setOnClickListener(v -> handleChooseTrackClicked());
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RequestCodes.CHOOSE_GPX_RESULT_CODE && resultCode == Activity.RESULT_OK) {
Uri fileUri = data.getData();
if (fileUri == null) {
return;
}
String gpxFile = fileUri.toString();
new StringPreference(
sharedPreferences, SharedPrefsKeys.GPX_FILE).set(gpxFile);
persistPermissionOnFile(data, app.getContentResolver());
updateGpxFileName();
}
}
private void updateStorageGraph() {
float usedPercentage =
(float) storageLocation.getUsedSpace() / storageLocation.getTotalSizeBytes();
long tileSize = storageLocation.getCacheSize();
float tilePercentage = (float) tileSize / storageLocation.getTotalSizeBytes();
binding.settingsCacheUsedSpaceText.setText(String.format(getString(R.string.settings_cache_used_mb),
Formatter.formatShortFileSize(getActivity(), storageLocation.getUsedSpace())));
binding.settingsCacheUsedCacheSpaceText.setText(String.format(getString(R.string.settings_cache_cache_mb),
Formatter.formatShortFileSize(getActivity(), tileSize)));
binding.settingsCacheFreeSpaceText.setText(String.format(getString(R.string.settings_cache_free_mb),
Formatter.formatShortFileSize(getActivity(), storageLocation.getFreeSpaceBytes())));
binding.settingsCacheStoragespacegraph.setBarPercentagesAnimated(
usedPercentage, tilePercentage);
}
private void updateClearCachePref() {
long currentSize = storageLocation.getCacheSize();
Timber.d("Current cache size: %s",
Formatter.formatShortFileSize(getActivity(), currentSize));
binding.settingsClearCacheSummaryText.setText(
String.format(getString(R.string.settings_cache_currently_used),
Formatter.formatShortFileSize(getActivity(), currentSize)));
}
@SuppressLint("Range")
private void updateGpxFileName() {
String gpxFile = new StringPreference(
sharedPreferences, SharedPrefsKeys.GPX_FILE).get();
String filename = gpxFile;
Cursor fileCursor = getContext().getContentResolver().query(Uri.parse(gpxFile), null, null, null);
if (fileCursor != null) {
fileCursor.moveToFirst();
filename = fileCursor.getString(fileCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
fileCursor.close();
}
binding.settingsChooseGpxSummaryText.setText(filename);
}
void handleClearCacheClicked() {
OfflineManager.getInstance(getActivity()).clearAmbientCache(new OfflineManager.FileSourceCallback() {
@Override
public void onSuccess() {
updateClearCachePref();
updateStorageGraph();
}
@Override
public void onError(@NonNull String s) {
Timber.e("Error clearing cache: %s", s);
}
});
}
void handleShowOnLockscreenChecked(boolean isChecked) {
new BooleanPreference(
sharedPreferences, SharedPrefsKeys.SHOW_ON_LOCKSCREEN).set(isChecked);
}
void handleKeepScreenOnChecked(boolean isChecked) {
new BooleanPreference(
sharedPreferences, SharedPrefsKeys.KEEP_SCREEN_ON).set(isChecked);
}
void handleShowTrack(boolean isChecked) {
new BooleanPreference(
sharedPreferences, SharedPrefsKeys.SHOW_GPX).set(isChecked);
}
void handleChooseTrackClicked() {
new ChooseGpxFileHandler(this).openChooser();
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
}