[android] Refactor system location settings invocation

- Add application settings as one more option
- Remove "Connection settings" buttons if no dialogs are available
- Move intent creation code to Utils

Signed-off-by: Roman Tsisyk <roman@tsisyk.com>
This commit is contained in:
Roman Tsisyk 2022-02-12 11:14:57 +03:00
parent 17696c07f2
commit d9671ad0cc
3 changed files with 35 additions and 37 deletions

View file

@ -1850,23 +1850,23 @@ public class MwmActivity extends BaseMwmFragmentActivity
@Override
public void onLocationError()
{
if (mLocationErrorDialogAnnoying)
if (mLocationErrorDialogAnnoying || (mLocationErrorDialog != null && mLocationErrorDialog.isShowing()))
return;
Intent intent = makeAppSettingsLocationIntent();
if (intent == null)
return;
showLocationErrorDialog(intent);
}
private Intent makeAppSettingsLocationIntent()
{
Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
if (intent.resolveActivity(getPackageManager()) != null)
return intent;
intent = new Intent(android.provider.Settings.ACTION_SECURITY_SETTINGS);
return intent.resolveActivity(getPackageManager()) == null ? null : intent;
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle(R.string.enable_location_services)
.setMessage(R.string.location_is_disabled_long_text)
.setOnCancelListener(dialog -> mLocationErrorDialogAnnoying = true)
.setNegativeButton(R.string.close, (dialog, which) -> mLocationErrorDialogAnnoying = true);
final Intent intent = Utils.makeSystemLocationSettingIntent(this);
if (intent != null)
{
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
builder.setPositiveButton(R.string.connection_settings, (dialog, which) -> startActivity(intent));
}
mLocationErrorDialog = builder.show();
}
@Override
@ -1894,26 +1894,6 @@ public class MwmActivity extends BaseMwmFragmentActivity
}
}
private void showLocationErrorDialog(@NonNull final Intent intent)
{
if (mLocationErrorDialog != null && mLocationErrorDialog.isShowing())
return;
mLocationErrorDialog = new AlertDialog.Builder(this)
.setTitle(R.string.enable_location_services)
.setMessage(R.string.location_is_disabled_long_text)
.setNegativeButton(R.string.close, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
mLocationErrorDialogAnnoying = true;
}
})
.setOnCancelListener(dialog -> mLocationErrorDialogAnnoying = true)
.setPositiveButton(R.string.connection_settings, (dialog, which) -> startActivity(intent)).show();
}
@Override
public void onLocationNotFound()
{

View file

@ -7,6 +7,8 @@ import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import androidx.annotation.NonNull;
import com.mapswithme.util.concurrency.UiThread;
import java.util.ArrayList;
@ -17,7 +19,7 @@ public class InputUtils
private InputUtils() { /* static class */ }
public static boolean isVoiceInputSupported(Context context)
public static boolean isVoiceInputSupported(@NonNull Context context)
{
if (mVoiceInputSupported == null)
mVoiceInputSupported = Utils.isIntentSupported(context, new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH));

View file

@ -147,11 +147,27 @@ public class Utils
showSnackbarAbove(view, viewAbove, message);
}
public static boolean isIntentSupported(Context context, Intent intent)
public static boolean isIntentSupported(@NonNull Context context, @NonNull Intent intent)
{
return context.getPackageManager().resolveActivity(intent, 0) != null;
}
public static @Nullable Intent makeSystemLocationSettingIntent(@NonNull Context context)
{
Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
if (isIntentSupported(context, intent))
return intent;
intent = new Intent(android.provider.Settings.ACTION_SECURITY_SETTINGS);
if (isIntentSupported(context, intent))
return intent;
intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", context.getPackageName(), null);
intent.setData(uri);
if (isIntentSupported(context, intent))
return intent;
return null;
}
public static void checkNotNull(Object object)
{
if (null == object) throw new NullPointerException("Argument here must not be NULL");