-
-
Notifications
You must be signed in to change notification settings - Fork 741
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
52 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright 2012 - 2017 Anton Tananaev ([email protected]) | ||
* Copyright 2012 - 2019 Anton Tananaev ([email protected]) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
|
@@ -27,24 +27,27 @@ | |
import android.net.Uri; | ||
import android.os.Build; | ||
import android.os.Bundle; | ||
import android.preference.EditTextPreference; | ||
import android.preference.Preference; | ||
import android.preference.PreferenceFragment; | ||
import android.preference.PreferenceManager; | ||
import android.preference.TwoStatePreference; | ||
import androidx.annotation.NonNull; | ||
import androidx.core.content.ContextCompat; | ||
import androidx.appcompat.app.AlertDialog; | ||
import androidx.preference.EditTextPreference; | ||
import androidx.preference.Preference; | ||
import androidx.preference.PreferenceFragmentCompat; | ||
import androidx.preference.PreferenceManager; | ||
import androidx.preference.TwoStatePreference; | ||
|
||
import android.util.Log; | ||
import android.view.Menu; | ||
import android.view.MenuInflater; | ||
import android.view.MenuItem; | ||
import android.webkit.URLUtil; | ||
import android.widget.Toast; | ||
|
||
import java.util.HashSet; | ||
import java.util.Random; | ||
import java.util.Set; | ||
|
||
public class MainFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener { | ||
public class MainFragment extends PreferenceFragmentCompat implements OnSharedPreferenceChangeListener { | ||
|
||
private static final String TAG = MainFragment.class.getSimpleName(); | ||
|
||
|
@@ -66,15 +69,14 @@ public class MainFragment extends PreferenceFragment implements OnSharedPreferen | |
private PendingIntent alarmIntent; | ||
|
||
@Override | ||
public void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
if (BuildConfig.HIDDEN_APP) { | ||
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { | ||
if (BuildConfig.HIDDEN_APP && Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { | ||
removeLauncherIcon(); | ||
} | ||
|
||
setHasOptionsMenu(true); | ||
|
||
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); | ||
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext()); | ||
addPreferencesFromResource(R.xml.preferences); | ||
initPreferences(); | ||
|
||
|
@@ -129,6 +131,7 @@ public boolean onPreferenceChange(Preference preference, Object newValue) { | |
if (sharedPreferences.getBoolean(KEY_STATUS, false)) { | ||
startTrackingService(true, false); | ||
} | ||
|
||
} | ||
|
||
private void removeLauncherIcon() { | ||
|
@@ -212,24 +215,31 @@ private void initPreferences() { | |
|
||
private void startTrackingService(boolean checkPermission, boolean permission) { | ||
if (checkPermission) { | ||
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { | ||
permission = true; | ||
} else { | ||
Set<String> requiredPermissions = new HashSet<>(); | ||
if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { | ||
requiredPermissions.add(Manifest.permission.ACCESS_FINE_LOCATION); | ||
} | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q | ||
&& ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_BACKGROUND_LOCATION) != PackageManager.PERMISSION_GRANTED) { | ||
requiredPermissions.add(Manifest.permission.ACCESS_BACKGROUND_LOCATION); | ||
} | ||
permission = requiredPermissions.isEmpty(); | ||
if (!permission) { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | ||
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSIONS_REQUEST_LOCATION); | ||
requestPermissions(requiredPermissions.toArray(new String[requiredPermissions.size()]), PERMISSIONS_REQUEST_LOCATION); | ||
} | ||
return; | ||
} | ||
} | ||
|
||
if (permission) { | ||
setPreferencesEnabled(false); | ||
ContextCompat.startForegroundService(getActivity(), new Intent(getActivity(), TrackingService.class)); | ||
ContextCompat.startForegroundService(getContext(), new Intent(getActivity(), TrackingService.class)); | ||
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, | ||
ALARM_MANAGER_INTERVAL, ALARM_MANAGER_INTERVAL, alarmIntent); | ||
} else { | ||
sharedPreferences.edit().putBoolean(KEY_STATUS, false).apply(); | ||
TwoStatePreference preference = (TwoStatePreference) findPreference(KEY_STATUS); | ||
TwoStatePreference preference = findPreference(KEY_STATUS); | ||
preference.setChecked(false); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters