|
67 | 67 | import org.coolreader.crengine.ReaderView; |
68 | 68 | import org.coolreader.crengine.ReaderViewLayout; |
69 | 69 | import org.coolreader.crengine.Services; |
| 70 | +import org.coolreader.crengine.Settings; |
70 | 71 | import org.coolreader.crengine.StrUtils; |
71 | 72 | import org.coolreader.crengine.TTS; |
72 | 73 | import org.coolreader.crengine.TTS.OnTTSCreatedListener; |
|
93 | 94 | import android.Manifest; |
94 | 95 | import android.annotation.TargetApi; |
95 | 96 | import android.app.Activity; |
| 97 | +import android.app.AlertDialog; |
96 | 98 | import android.content.BroadcastReceiver; |
97 | 99 | import android.content.ComponentName; |
98 | 100 | import android.content.Context; |
|
118 | 120 | import android.os.Bundle; |
119 | 121 | import android.os.Debug; |
120 | 122 | import android.os.Environment; |
| 123 | +import android.support.v4.app.ActivityCompat; |
| 124 | +import android.support.v4.content.ContextCompat; |
121 | 125 | import android.support.v4.provider.DocumentFile; |
122 | 126 | import android.view.LayoutInflater; |
123 | 127 | import android.view.View; |
@@ -145,6 +149,7 @@ public class CoolReader extends BaseActivity implements SensorEventListener |
145 | 149 | public static final Logger log = L.create("cr"); |
146 | 150 |
|
147 | 151 | public static int REQUEST_CODE_OPEN_DOCUMENT_TREE = 200001; |
| 152 | + public static int REQUEST_CODE_LOCATION_PERMISSION = 200002; |
148 | 153 | public Uri sdCardUri = null; |
149 | 154 | private FileInfo fileToDelete = null; |
150 | 155 |
|
@@ -1060,8 +1065,11 @@ protected void onResume() { |
1060 | 1065 | mSensorManager.registerListener(deviceOrientation.getEventListener(), accelerometer, SensorManager.SENSOR_DELAY_UI); |
1061 | 1066 | mSensorManager.registerListener(deviceOrientation.getEventListener(), magnetometer, SensorManager.SENSOR_DELAY_UI); |
1062 | 1067 |
|
1063 | | - geoLastData.gps.start(geoLastData.geoListener); |
1064 | | - geoLastData.netw.start(geoLastData.netwListener); |
| 1068 | + int iGeo = settings().getInt(Settings.PROP_APP_GEO, 0); |
| 1069 | + if (iGeo>1) { |
| 1070 | + geoLastData.gps.start(geoLastData.geoListener); |
| 1071 | + geoLastData.netw.start(geoLastData.netwListener); |
| 1072 | + } |
1065 | 1073 |
|
1066 | 1074 | if(mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).size()!=0){ |
1067 | 1075 | Sensor s = mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0); |
@@ -3200,107 +3208,40 @@ public void onUpdate(Location oldLoc, long oldTime, Location newLoc, |
3200 | 3208 | } |
3201 | 3209 |
|
3202 | 3210 | }; |
3203 | | - |
3204 | | - String s =""; |
3205 | | - try { |
3206 | | - InputStream is = getResources().openRawResource(R.raw.metro_coords); |
3207 | | - BufferedReader reader = new BufferedReader(new InputStreamReader(is)); |
3208 | | - String str = ""; |
3209 | | - while ((str = reader.readLine()) != null) s=s + " " +str; |
3210 | | - is.close(); |
3211 | | - } catch (Exception e) { |
3212 | | -// showToast("Could not parse genres from file: genres_rus.txt. "+ |
3213 | | -// e.getClass().getSimpleName()+" "+e.getMessage()); |
3214 | | - } |
3215 | | - if (geoLastData.metroLocations == null) geoLastData.metroLocations = new ArrayList<MetroLocation>(); |
3216 | | - geoLastData.metroLocations.clear(); |
3217 | | - try { |
3218 | | - JSONArray jsonA = new JSONArray(s); |
3219 | | - if (jsonA != null) { |
3220 | | - int i = 0; |
3221 | | - while (i < jsonA.length()) { |
3222 | | - JSONObject jso = (JSONObject) jsonA.get(i); |
3223 | | - MetroLocation metroLocation = new MetroLocation(); |
3224 | | - if (jso.has("id")) { metroLocation.id = jso.getInt("id"); } |
3225 | | - if (jso.has("name")) { metroLocation.name = jso.getString("name"); } |
3226 | | - if (jso.has("url")) { metroLocation.url = jso.getString("url"); } |
3227 | | - ArrayList<MetroLine> metroLines = null; |
3228 | | - if (jso.has("lines")) { |
3229 | | - JSONArray jsonL = jso.getJSONArray("lines"); |
3230 | | - metroLines = new ArrayList<MetroLine>(); |
3231 | | - int j = 0; |
3232 | | - while (j < jsonL.length()) { |
3233 | | - JSONObject jsoL = (JSONObject) jsonL.get(j); |
3234 | | - MetroLine metroLine = new MetroLine(); |
3235 | | - if (jsoL.has("id")) { metroLine.id = jsoL.getInt("id"); } |
3236 | | - if (jsoL.has("hex_color")) { metroLine.hexColor = jsoL.getString("hex_color"); } |
3237 | | - if (jsoL.has("name")) { metroLine.name = jsoL.getString("name"); } |
3238 | | - ArrayList<MetroStation> metroStations = null; |
3239 | | - if (jsoL.has("stations")) { |
3240 | | - JSONArray jsonS = jsoL.getJSONArray("stations"); |
3241 | | - metroStations = new ArrayList<MetroStation>(); |
3242 | | - int k = 0; |
3243 | | - while (k < jsonS.length()) { |
3244 | | - JSONObject jsoS = (JSONObject) jsonS.get(k); |
3245 | | - MetroStation metroStation = new MetroStation(); |
3246 | | - if (jsoS.has("id")) { metroStation.id = jsoS.getDouble("id"); } |
3247 | | - if (jsoS.has("name")) { metroStation.name = jsoS.getString("name"); } |
3248 | | - if (metroStation.name.contains("окоссов")) geoLastData.tempStation = metroStation; // !!!! |
3249 | | - if (jsoS.has("lat")) { metroStation.lat = jsoS.getDouble("lat"); } |
3250 | | - if (jsoS.has("lng")) { metroStation.lon = jsoS.getDouble("lng"); } |
3251 | | - if (jsoS.has("order")) { metroStation.order = jsoS.getInt("order"); } |
3252 | | - metroStations.add(metroStation); |
3253 | | - k++; |
3254 | | - } |
3255 | | - metroLine.metroStations = metroStations; |
3256 | | - } |
3257 | | - metroLines.add(metroLine); |
3258 | | - j++; |
3259 | | - } |
| 3211 | + int iGeo = settings().getInt(Settings.PROP_APP_GEO, 0); |
| 3212 | + if ((iGeo==2)||(iGeo==4)) geoLastData.loadMetroStations(this); |
| 3213 | + if ((iGeo==3)||(iGeo==4)) geoLastData.loadTransportStops(this); |
| 3214 | + if (iGeo>1) { |
| 3215 | + geoLastData.gps.start(geoLastData.geoListener); |
| 3216 | + geoLastData.netw.start(geoLastData.netwListener); |
| 3217 | + } |
| 3218 | + } |
| 3219 | + |
| 3220 | + public boolean checkLocationPermission() { |
| 3221 | + if (ContextCompat.checkSelfPermission(this, |
| 3222 | + Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { |
| 3223 | + if (ActivityCompat.shouldShowRequestPermissionRationale(this, |
| 3224 | + Manifest.permission.ACCESS_FINE_LOCATION)) { |
| 3225 | + new AlertDialog.Builder(this) |
| 3226 | + .setTitle(R.string.title_location_permission) |
| 3227 | + .setMessage(R.string.text_location_permission) |
| 3228 | + .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { |
| 3229 | + @Override |
| 3230 | + public void onClick(DialogInterface dialogInterface, int i) { |
| 3231 | + ActivityCompat.requestPermissions(CoolReader.this, |
| 3232 | + new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, |
| 3233 | + REQUEST_CODE_LOCATION_PERMISSION); |
3260 | 3234 | } |
3261 | | - metroLocation.metroLines = metroLines; |
3262 | | - geoLastData.metroLocations.add(metroLocation); |
3263 | | - i++; |
3264 | | - } |
3265 | | - } |
3266 | | - } catch (JSONException e) { |
3267 | | - |
3268 | | - } |
3269 | | - if (geoLastData.transportStops == null) geoLastData.transportStops = new ArrayList<TransportStop>(); |
3270 | | - geoLastData.transportStops.clear(); |
3271 | | - String sT =""; |
3272 | | - try { |
3273 | | - InputStream is = getResources().openRawResource(R.raw.data_398); |
3274 | | - BufferedReader reader = new BufferedReader(new InputStreamReader(is, "windows-1251")); |
3275 | | - String str = ""; |
3276 | | - while ((str = reader.readLine()) != null) sT=sT + " " +str; |
3277 | | - is.close(); |
3278 | | - } catch (Exception e) { |
3279 | | - e.printStackTrace(); |
3280 | | - } |
3281 | | - try { |
3282 | | - JSONArray jsonA = new JSONArray(sT); |
3283 | | - if (jsonA != null) { |
3284 | | - int i = 0; |
3285 | | - while (i < jsonA.length()) { |
3286 | | - JSONObject jso = (JSONObject) jsonA.get(i); |
3287 | | - TransportStop transportStop = new TransportStop(); |
3288 | | - if (jso.has("Street")) { transportStop.street = jso.getString("Street"); } |
3289 | | - if (jso.has("Name")) { transportStop.name = jso.getString("Name"); } |
3290 | | - if (jso.has("Latitude_WGS84")) { transportStop.lat = jso.getDouble("Latitude_WGS84"); } |
3291 | | - if (jso.has("Longitude_WGS84")) { transportStop.lon = jso.getDouble("Longitude_WGS84"); } |
3292 | | - if (jso.has("District")) { transportStop.district = jso.getString("District"); } |
3293 | | - if (jso.has("RouteNumbers")) { transportStop.routeNumbers = jso.getString("RouteNumbers"); } |
3294 | | - geoLastData.transportStops.add(transportStop); |
3295 | | - geoLastData.tempStop = transportStop; |
3296 | | - i++; |
3297 | | - } |
| 3235 | + }).create().show(); |
| 3236 | + } else { |
| 3237 | + ActivityCompat.requestPermissions(CoolReader.this, |
| 3238 | + new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, |
| 3239 | + REQUEST_CODE_LOCATION_PERMISSION); |
3298 | 3240 | } |
3299 | | - } catch (JSONException e) { |
3300 | | - |
| 3241 | + return false; |
| 3242 | + } else { |
| 3243 | + return true; |
3301 | 3244 | } |
3302 | | - geoLastData.gps.start(geoLastData.geoListener); |
3303 | | - geoLastData.netw.start(geoLastData.netwListener); |
3304 | 3245 | } |
3305 | 3246 |
|
3306 | 3247 | } |
|
0 commit comments