Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0b5c4233 authored by 2bllw8's avatar 2bllw8 Committed by luca020400
Browse files

Recorder: move preferences management to its own class

Change-Id: I7deb59c26ecbf38eab91f36344335cc1a33171c0
parent fac94751
Loading
Loading
Loading
Loading
+10 −8
Original line number Diff line number Diff line
@@ -24,12 +24,13 @@ import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;

import org.lineageos.recorder.utils.PermissionManager;
import org.lineageos.recorder.utils.Utils;
import org.lineageos.recorder.utils.PreferencesManager;

public class DialogActivity extends AppCompatActivity {
    public static final String EXTRA_IS_RECORDING = "is_recording";

    private PermissionManager mPermissionManager;
    private PreferencesManager mPreferences;
    private SwitchCompat mLocationSwitch;

    @Override
@@ -37,6 +38,7 @@ public class DialogActivity extends AppCompatActivity {
        super.onCreate(savedInstance);

        mPermissionManager = new PermissionManager(this);
        mPreferences = new PreferencesManager(this);

        setFinishOnTouchOutside(true);

@@ -88,12 +90,12 @@ public class DialogActivity extends AppCompatActivity {
    private void setupLocationSwitch(@NonNull SwitchCompat locationSwitch,
                                     boolean isRecording) {
        final boolean tagWithLocation;
        if (Utils.getTagWithLocation(this)) {
        if (mPreferences.getTagWithLocation()) {
            if (mPermissionManager.hasLocationPermission()) {
                tagWithLocation = true;
            } else {
                // Permission revoked -> disabled feature
                Utils.setTagWithLocation(this, false);
                mPreferences.setTagWithLocation(false);
                tagWithLocation = false;
            }
        } else {
@@ -108,12 +110,12 @@ public class DialogActivity extends AppCompatActivity {
            locationSwitch.setOnCheckedChangeListener((button, isChecked) -> {
                if (isChecked) {
                    if (mPermissionManager.hasLocationPermission()) {
                        Utils.setTagWithLocation(this, true);
                        mPreferences.setTagWithLocation(true);
                    } else {
                        mPermissionManager.requestLocationPermission();
                    }
                } else {
                    Utils.setTagWithLocation(this, false);
                    mPreferences.setTagWithLocation(false);
                }
            });
        }
@@ -121,19 +123,19 @@ public class DialogActivity extends AppCompatActivity {

    private void setupHighQualitySwitch(@NonNull SwitchCompat highQualitySwitch,
                                        boolean isRecording) {
        final boolean highQuality = Utils.getRecordInHighQuality(this);
        final boolean highQuality = mPreferences.getRecordInHighQuality();
        highQualitySwitch.setChecked(highQuality);

        if (isRecording) {
            highQualitySwitch.setEnabled(false);
        } else {
            highQualitySwitch.setOnCheckedChangeListener((button, isChecked) ->
                    Utils.setRecordingHighQuality(this, isChecked));
                    mPreferences.setRecordingHighQuality(isChecked));
        }
    }

    private void toggleAfterPermissionRequest() {
        mLocationSwitch.setChecked(true);
        Utils.setTagWithLocation(this, true);
        mPreferences.setTagWithLocation(true);
    }
}
+4 −2
Original line number Diff line number Diff line
@@ -51,7 +51,7 @@ import org.lineageos.recorder.status.UiStatus;
import org.lineageos.recorder.task.AddRecordingToContentProviderTask;
import org.lineageos.recorder.task.TaskExecutor;
import org.lineageos.recorder.utils.LastRecordHelper;
import org.lineageos.recorder.utils.Utils;
import org.lineageos.recorder.utils.PreferencesManager;

import java.io.IOException;
import java.lang.ref.WeakReference;
@@ -90,6 +90,7 @@ public class SoundRecorderService extends Service {
    private static final String NOTIFICATION_CHANNEL = "soundrecorder_notification_channel";

    private NotificationManager mNotificationManager;
    private PreferencesManager mPreferencesManager;
    private TaskExecutor mTaskExecutor;

    private final Object mLock = new Object();
@@ -152,6 +153,7 @@ public class SoundRecorderService extends Service {
            createNotificationChannel();
        }

        mPreferencesManager = new PreferencesManager(this);
        mTaskExecutor = new TaskExecutor();
    }

@@ -191,7 +193,7 @@ public class SoundRecorderService extends Service {
            return false;
        }

        mRecorder = Utils.getRecordInHighQuality(this)
        mRecorder = mPreferencesManager.getRecordInHighQuality()
                ? new HighQualityRecorder()
                : new GoodQualityRecorder();

+3 −1
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ public final class LocationHelper {

    @NonNull
    private final Context context;
    private final PreferencesManager preferences;

    @Nullable
    private final LocationManager locationManager;
@@ -41,6 +42,7 @@ public final class LocationHelper {
        this.context = context;

        locationManager = context.getSystemService(LocationManager.class);
        preferences = new PreferencesManager(context);
    }

    @Nullable
@@ -77,7 +79,7 @@ public final class LocationHelper {

    @Nullable
    private Location getLastGoodLocation() {
        if (locationManager == null || !Utils.getTagWithLocation(context)) {
        if (locationManager == null || !preferences.getTagWithLocation()) {
            return null;
        }
        Location lastGoodLocation;
+6 −14
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@
package org.lineageos.recorder.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
@@ -28,8 +27,6 @@ import androidx.annotation.NonNull;
import org.lineageos.recorder.R;

public final class OnBoardingHelper {
    private static final String ONBOARD_SETTINGS = "onboard_settings";
    private static final String ONBOARD_SOUND_LIST = "onboard_list";
    private static final long RIPPLE_DELAY = 500;
    private static final long RIPPLE_REPEAT = 4;
    private static final int RIPPLE_OPEN_APP_WAIT = 1;
@@ -40,12 +37,12 @@ public final class OnBoardingHelper {
    }

    public static void onBoardList(Context context, View view) {
        SharedPreferences prefs = getPrefs(context);
        int appOpenTimes = prefs.getInt(ONBOARD_SOUND_LIST, 0);
        final PreferencesManager prefsManager = new PreferencesManager(context);
        final int appOpenTimes = prefsManager.getOnboardListCounter();

        // Wait for the user to open the app 2 times before exposing this
        if (appOpenTimes <= RIPPLE_OPEN_APP_WAIT) {
            prefs.edit().putInt(ONBOARD_SOUND_LIST, appOpenTimes + 1).apply();
            prefsManager.setOnboardListCounter(appOpenTimes + 1);
        }

        if (appOpenTimes == RIPPLE_OPEN_APP_WAIT) {
@@ -58,12 +55,12 @@ public final class OnBoardingHelper {
    }

    public static void onBoardSettings(Context context, View view) {
        SharedPreferences prefs = getPrefs(context);
        int appOpenTimes = prefs.getInt(ONBOARD_SETTINGS, 0);
        final PreferencesManager prefsManager = new PreferencesManager(context);
        final int appOpenTimes = prefsManager.getOnboardSettingsCounter();

        // Wait for the user to open the app 4 times before exposing this
        if (appOpenTimes <= ROTATION_OPEN_APP_WAIT) {
            prefs.edit().putInt(ONBOARD_SETTINGS, appOpenTimes + 1).apply();
            prefsManager.setOnboardSettingsCounter(appOpenTimes + 1);
        }

        if (appOpenTimes == ROTATION_OPEN_APP_WAIT) {
@@ -72,11 +69,6 @@ public final class OnBoardingHelper {
        }
    }

    @NonNull
    private static SharedPreferences getPrefs(@NonNull Context context) {
        return context.getSharedPreferences(Utils.PREFS, 0);
    }

    @NonNull
    private static Runnable pressRipple(View view) {
        return () -> {
+73 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The LineageOS Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.lineageos.recorder.utils;

import android.content.Context;
import android.content.SharedPreferences;

public final class PreferencesManager {
    private static final String PREFS = "preferences";
    private static final String PREF_TAG_WITH_LOCATION = "tag_with_location";
    private static final String PREF_RECORDING_QUALITY = "recording_quality";
    private static final String PREF_ONBOARD_SETTINGS_COUNTER = "onboard_settings";
    private static final String PREF_ONBOARD_SOUND_LIST_COUNTER = "onboard_list";

    private final SharedPreferences mPreferences;

    public PreferencesManager(Context context) {
        mPreferences = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE);
    }

    public void setRecordingHighQuality(boolean highQuality) {
        mPreferences.edit()
                .putInt(PREF_RECORDING_QUALITY, highQuality ? 1 : 0)
                .apply();
    }

    public boolean getRecordInHighQuality() {
        return mPreferences.getInt(PREF_RECORDING_QUALITY, 0) == 1;
    }

    public void setTagWithLocation(boolean tagWithLocation) {
        mPreferences.edit()
                .putBoolean(PREF_TAG_WITH_LOCATION, tagWithLocation)
                .apply();
    }

    public boolean getTagWithLocation() {
        return mPreferences.getBoolean(PREF_TAG_WITH_LOCATION, false);
    }

    public int getOnboardSettingsCounter() {
        return mPreferences.getInt(PREF_ONBOARD_SETTINGS_COUNTER, 0);
    }

    public void setOnboardSettingsCounter(int value) {
        mPreferences.edit()
                .putInt(PREF_ONBOARD_SETTINGS_COUNTER, value)
                .apply();
    }

    public int getOnboardListCounter() {
        return mPreferences.getInt(PREF_ONBOARD_SOUND_LIST_COUNTER, 0);
    }

    public void setOnboardListCounter(int value) {
        mPreferences.edit()
                .putInt(PREF_ONBOARD_SOUND_LIST_COUNTER, value)
                .apply();
    }
}
Loading