Search in sources :

Example 1 with PERMISSION_LOCATION

use of org.openbot.utils.Constants.PERMISSION_LOCATION in project OpenBot by isl-org.

the class SettingsFragment method onCreatePreferences.

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    setPreferencesFromResource(R.xml.root_preferences, rootKey);
    mViewModel = new ViewModelProvider(requireActivity()).get(MainViewModel.class);
    vehicle = mViewModel.getVehicle().getValue();
    connection = findPreference("connection");
    if (connection != null) {
        connection.setTitle("No Device");
        if (vehicle != null && vehicle.isUsbConnected()) {
            connection.setChecked(true);
            connection.setTitle(vehicle.getUsbConnection().getProductName());
        } else {
            connection.setTitle("No Device");
            connection.setChecked(false);
        }
        connection.setOnPreferenceClickListener(preference -> {
            Timber.d(String.valueOf(connection.isChecked()));
            if (vehicle != null) {
                if (connection.isChecked()) {
                    vehicle.connectUsb();
                    if (vehicle.isUsbConnected())
                        connection.setTitle(vehicle.getUsbConnection().getProductName());
                    else {
                        connection.setTitle("No Device");
                        connection.setChecked(false);
                        Toast.makeText(requireContext().getApplicationContext(), "Please check the USB connection.", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    vehicle.disconnectUsb();
                    connection.setTitle("No Device");
                }
                mViewModel.setUsbStatus(vehicle.isUsbConnected());
            }
            return true;
        });
    }
    camera = findPreference("camera");
    if (camera != null) {
        camera.setChecked(PermissionUtils.hasCameraPermission(requireActivity()));
        camera.setOnPreferenceChangeListener((preference, newValue) -> {
            if (camera.isChecked())
                startInstalledAppDetailsActivity(requireActivity());
            else {
                if (!PermissionUtils.shouldShowRational(requireActivity(), Constants.PERMISSION_CAMERA)) {
                    startInstalledAppDetailsActivity(requireActivity());
                } else {
                    requestPermissionLauncher.launch(new String[] { Constants.PERMISSION_CAMERA });
                }
            }
            return false;
        });
    }
    storage = findPreference("storage");
    if (storage != null) {
        storage.setChecked(PermissionUtils.hasStoragePermission(requireActivity()));
        storage.setOnPreferenceChangeListener((preference, newValue) -> {
            if (storage.isChecked())
                startInstalledAppDetailsActivity(requireActivity());
            else {
                if (!PermissionUtils.shouldShowRational(requireActivity(), Constants.PERMISSION_STORAGE)) {
                    startInstalledAppDetailsActivity(requireActivity());
                } else
                    requestPermissionLauncher.launch(new String[] { Constants.PERMISSION_STORAGE });
            }
            return false;
        });
    }
    location = findPreference("location");
    if (location != null) {
        location.setChecked(PermissionUtils.hasLocationPermission(requireActivity()));
        location.setOnPreferenceChangeListener((preference, newValue) -> {
            if (location.isChecked())
                startInstalledAppDetailsActivity(requireActivity());
            else {
                if (!PermissionUtils.shouldShowRational(requireActivity(), PERMISSION_LOCATION)) {
                    startInstalledAppDetailsActivity(requireActivity());
                } else
                    requestPermissionLauncher.launch(new String[] { PERMISSION_LOCATION });
            }
            return false;
        });
    }
    mic = findPreference("mic");
    if (mic != null) {
        mic.setChecked(PermissionUtils.hasAudioPermission(requireActivity()));
        mic.setOnPreferenceChangeListener((preference, newValue) -> {
            if (mic.isChecked())
                startInstalledAppDetailsActivity(requireActivity());
            else {
                if (!PermissionUtils.shouldShowRational(requireActivity(), Constants.PERMISSION_AUDIO)) {
                    startInstalledAppDetailsActivity(requireActivity());
                } else
                    requestPermissionLauncher.launch(new String[] { Constants.PERMISSION_AUDIO });
            }
            return false;
        });
    }
    ListPreference streamMode = findPreference("video_server");
    if (streamMode != null)
        streamMode.setOnPreferenceChangeListener((preference, newValue) -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
            builder.setTitle(R.string.confirm_title);
            builder.setMessage(R.string.stream_change_body);
            builder.setPositiveButton("Yes", (dialog, id) -> {
                streamMode.setValue(newValue.toString());
                restartApp();
            });
            builder.setNegativeButton("Cancel", (dialog, id) -> streamMode.setValue(streamMode.getEntry().toString()));
            AlertDialog dialog = builder.create();
            dialog.show();
            return false;
        });
}
Also used : Bundle(android.os.Bundle) AlertDialog(androidx.appcompat.app.AlertDialog) PackageManager(android.content.pm.PackageManager) NonNull(androidx.annotation.NonNull) Uri(android.net.Uri) Intent(android.content.Intent) PreferenceFragmentCompat(androidx.preference.PreferenceFragmentCompat) Handler(android.os.Handler) Toast(android.widget.Toast) View(android.view.View) PERMISSION_AUDIO(org.openbot.utils.Constants.PERMISSION_AUDIO) Settings(android.provider.Settings) PERMISSION_STORAGE(org.openbot.utils.Constants.PERMISSION_STORAGE) R(org.openbot.R) PERMISSION_CAMERA(org.openbot.utils.Constants.PERMISSION_CAMERA) Vehicle(org.openbot.vehicle.Vehicle) ActivityResultLauncher(androidx.activity.result.ActivityResultLauncher) ViewModelProvider(androidx.lifecycle.ViewModelProvider) SwitchPreferenceCompat(androidx.preference.SwitchPreferenceCompat) Constants(org.openbot.utils.Constants) PERMISSION_LOCATION(org.openbot.utils.Constants.PERMISSION_LOCATION) Timber(timber.log.Timber) PermissionUtils(org.openbot.utils.PermissionUtils) Nullable(androidx.annotation.Nullable) ListPreference(androidx.preference.ListPreference) ActivityResultContracts(androidx.activity.result.contract.ActivityResultContracts) Activity(android.app.Activity) AlertDialog(androidx.appcompat.app.AlertDialog) ListPreference(androidx.preference.ListPreference) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Aggregations

Activity (android.app.Activity)1 Intent (android.content.Intent)1 PackageManager (android.content.pm.PackageManager)1 Uri (android.net.Uri)1 Bundle (android.os.Bundle)1 Handler (android.os.Handler)1 Settings (android.provider.Settings)1 View (android.view.View)1 Toast (android.widget.Toast)1 ActivityResultLauncher (androidx.activity.result.ActivityResultLauncher)1 ActivityResultContracts (androidx.activity.result.contract.ActivityResultContracts)1 NonNull (androidx.annotation.NonNull)1 Nullable (androidx.annotation.Nullable)1 AlertDialog (androidx.appcompat.app.AlertDialog)1 ViewModelProvider (androidx.lifecycle.ViewModelProvider)1 ListPreference (androidx.preference.ListPreference)1 PreferenceFragmentCompat (androidx.preference.PreferenceFragmentCompat)1 SwitchPreferenceCompat (androidx.preference.SwitchPreferenceCompat)1 R (org.openbot.R)1 Constants (org.openbot.utils.Constants)1