-
-
Notifications
You must be signed in to change notification settings - Fork 985
/
StartGPSStatus.java
144 lines (132 loc) · 5.07 KB
/
StartGPSStatus.java
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
package net.osmand.plus.activities.actions;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.AppCompatCheckBox;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.Version;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.utils.AndroidUtils;
import net.osmand.plus.utils.ColorUtilities;
import net.osmand.plus.utils.UiUtilities;
public class StartGPSStatus {
private OsmandApplication app;
private OsmandSettings settings;
private MapActivity mapActivity;
public StartGPSStatus(@NonNull MapActivity mapActivity) {
this.mapActivity = mapActivity;
this.app = mapActivity.getMyApplication();
this.settings = app.getSettings();
}
public void run() {
String appName = settings.GPS_STATUS_APP.get();
GpsStatusApps[] values = GpsStatusApps.values();
for (GpsStatusApps g : values) {
if (appName.length() > 0 && g.appName.equals(appName)) {
if (g.installed(mapActivity)) {
runChosenGPSStatus(g);
return;
} else {
settings.GPS_STATUS_APP.set("");
}
}
}
showDialog();
}
public void showDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(mapActivity);
builder.setTitle(R.string.gps_status);
LinearLayout ll = new LinearLayout(mapActivity);
ListView lv = new ListView(mapActivity);
int dp24 = AndroidUtils.dpToPx(mapActivity, 24f);
int dp12 = AndroidUtils.dpToPx(mapActivity, 12f);
int dp8 = AndroidUtils.dpToPx(mapActivity, 8f);
lv.setPadding(0, dp8, 0, dp8);
AppCompatCheckBox cb = new AppCompatCheckBox(mapActivity);
cb.setText(R.string.shared_string_remember_my_choice);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
AndroidUtils.setMargins(lp, dp24, dp8, dp8, dp24);
cb.setLayoutParams(lp);
cb.setPadding(dp8, 0, 0, 0);
int textColorPrimary = ColorUtilities.getPrimaryTextColor(mapActivity, isNightMode());
int selectedModeColor = settings.getApplicationMode().getProfileColor(isNightMode());
cb.setTextColor(textColorPrimary);
UiUtilities.setupCompoundButton(isNightMode(), selectedModeColor, cb);
final int layout = R.layout.list_menu_item_native;
ArrayAdapter<GpsStatusApps> adapter = new ArrayAdapter<GpsStatusApps>(mapActivity, layout, GpsStatusApps.values()) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Context themedCtx = UiUtilities.getThemedContext(mapActivity, isNightMode());
LayoutInflater inflater = UiUtilities.getInflater(themedCtx, isNightMode());
View v = inflater.inflate(layout, parent, false);
TextView tv = v.findViewById(R.id.title);
tv.setPadding(dp12, 0, dp24, 0);
tv.setText(getItem(position).stringRes);
v.findViewById(R.id.toggle_item).setVisibility(View.INVISIBLE);
return v;
}
};
lv.setAdapter(adapter);
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(lv);
ll.addView(cb);
AlertDialog dlg = builder.create();
lv.setOnItemClickListener((parent, view, position, id) -> {
boolean remember = cb.isChecked();
GpsStatusApps item = adapter.getItem(position);
if (remember) {
settings.GPS_STATUS_APP.set(item.appName);
}
dlg.dismiss();
runChosenGPSStatus(item);
});
dlg.setView(ll);
dlg.show();
}
private void runChosenGPSStatus(GpsStatusApps g) {
if (g.installed(mapActivity)) {
Intent intent = null;
PackageManager pm = mapActivity.getPackageManager();
try {
String appName = !g.paidAppName.isEmpty() &&
g.installed(mapActivity, g.paidAppName) ? g.paidAppName : g.appName;
intent = pm.getLaunchIntentForPackage(appName);
} catch (RuntimeException e) {
}
if (intent == null) {
return;
}
intent.addCategory(Intent.CATEGORY_LAUNCHER);
AndroidUtils.startActivityIfSafe(mapActivity, intent);
} else if (Version.isMarketEnabled()) {
AlertDialog.Builder builder = new AlertDialog.Builder(mapActivity);
builder.setMessage(mapActivity.getString(R.string.gps_status_app_not_found));
builder.setPositiveButton(mapActivity.getString(R.string.shared_string_yes), (dialog, which) -> {
Uri uri = Uri.parse(Version.getUrlWithUtmRef(app, g.appName));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
AndroidUtils.startActivityIfSafe(mapActivity, intent);
});
builder.setNegativeButton(mapActivity.getString(R.string.shared_string_no), null);
builder.show();
} else {
Toast.makeText(mapActivity, R.string.gps_status_app_not_found, Toast.LENGTH_LONG).show();
}
}
public boolean isNightMode() {
return app.getDaynightHelper().isNightModeForMapControls();
}
}