convert config to kotlin

This commit is contained in:
tibbi 2016-11-15 22:35:06 +01:00
parent 6c83203bbb
commit e4c4504c71
4 changed files with 85 additions and 134 deletions

View File

@ -1,127 +0,0 @@
package com.simplemobiletools.camera;
import android.content.Context;
import android.content.SharedPreferences;
import android.hardware.Camera;
import android.os.Environment;
public class Config {
private SharedPreferences mPrefs;
public static Config newInstance(Context context) {
return new Config(context);
}
public Config(Context context) {
mPrefs = context.getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE);
}
public boolean getIsFirstRun() {
return mPrefs.getBoolean(Constants.IS_FIRST_RUN, true);
}
public void setIsFirstRun(boolean firstRun) {
mPrefs.edit().putBoolean(Constants.IS_FIRST_RUN, firstRun).apply();
}
public boolean getIsDarkTheme() {
return mPrefs.getBoolean(Constants.IS_DARK_THEME, false);
}
public void setIsDarkTheme(boolean isDarkTheme) {
mPrefs.edit().putBoolean(Constants.IS_DARK_THEME, isDarkTheme).apply();
}
public String getSavePhotosFolder() {
return mPrefs.getString(Constants.SAVE_PHOTOS, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString());
}
public void setSavePhotosFolder(String path) {
mPrefs.edit().putString(Constants.SAVE_PHOTOS, path).apply();
}
public boolean getForceRatioEnabled() {
return mPrefs.getBoolean(Constants.FORCE_RATIO, true);
}
public void setForceRatioEnabled(boolean enabled) {
mPrefs.edit().putBoolean(Constants.FORCE_RATIO, enabled).apply();
}
// todo: delete this
public int getMaxResolution() {
return mPrefs.getInt(Constants.MAX_RESOLUTION, -1);
}
public int getMaxPhotoResolution() {
return mPrefs.getInt(Constants.MAX_PHOTO_RESOLUTION, getOldDefaultResolution());
}
public void setMaxPhotoResolution(int maxRes) {
mPrefs.edit().putInt(Constants.MAX_PHOTO_RESOLUTION, maxRes).apply();
}
private int getOldDefaultResolution() {
final int index = getMaxResolution();
switch (index) {
case 1:
return Constants.EIGHT_MPX;
case 2:
return 0;
default:
return Constants.FIVE_MPX;
}
}
public int getMaxVideoResolution() {
int maxRes = mPrefs.getInt(Constants.MAX_VIDEO_RESOLUTION, Constants.P720);
switch (maxRes) {
case 0:
maxRes = Constants.P480;
break;
case 1:
maxRes = Constants.P720;
break;
case 2:
maxRes = Constants.P1080;
break;
}
return maxRes;
}
public void setMaxVideoResolution(int maxRes) {
mPrefs.edit().putInt(Constants.MAX_VIDEO_RESOLUTION, maxRes).apply();
}
public boolean getIsSoundEnabled() {
return mPrefs.getBoolean(Constants.SOUND, true);
}
public void setIsSoundEnabled(boolean enabled) {
mPrefs.edit().putBoolean(Constants.SOUND, enabled).apply();
}
public int getLastUsedCamera() {
return mPrefs.getInt(Constants.LAST_USED_CAMERA, Camera.CameraInfo.CAMERA_FACING_BACK);
}
public void setLastUsedCamera(int cameraId) {
mPrefs.edit().putInt(Constants.LAST_USED_CAMERA, cameraId).apply();
}
public boolean getLastFlashlightState() {
return mPrefs.getBoolean(Constants.LAST_FLASHLIGHT_STATE, false);
}
public void setLastFlashlightState(boolean enabled) {
mPrefs.edit().putBoolean(Constants.LAST_FLASHLIGHT_STATE, enabled).apply();
}
public String getTreeUri() {
return mPrefs.getString(Constants.TREE_URI, "");
}
public void setTreeUri(String uri) {
mPrefs.edit().putString(Constants.TREE_URI, uri).apply();
}
}

View File

@ -158,7 +158,7 @@ public class Preview extends ViewGroup
initRecorder();
}
final Config config = Config.newInstance(mContext);
final Config config = Config.Companion.newInstance(mContext);
mForceAspectRatio = config.getForceRatioEnabled();
return true;
@ -275,7 +275,7 @@ public class Preview extends ViewGroup
mParameters.setPictureSize(maxSize.width, maxSize.height);
mParameters.setRotation(rotation % 360);
if (Config.newInstance(mContext).getIsSoundEnabled()) {
if (Config.Companion.newInstance(mContext).isSoundEnabled()) {
final AudioManager audioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
final int volume = audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM);
if (volume != 0) {
@ -319,7 +319,7 @@ public class Preview extends ViewGroup
};
private Camera.Size getOptimalPictureSize() {
final int maxResolution = Config.newInstance(mContext).getMaxPhotoResolution();
final int maxResolution = Config.Companion.newInstance(mContext).getMaxPhotoResolution();
final List<Camera.Size> sizes = mParameters.getSupportedPictureSizes();
Collections.sort(sizes, new SizesComparator());
Camera.Size maxSize = sizes.get(0);
@ -339,7 +339,7 @@ public class Preview extends ViewGroup
}
private int getMaxVideoResolution() {
final int maxRes = Config.newInstance(mContext).getMaxVideoResolution();
final int maxRes = Config.Companion.newInstance(mContext).getMaxVideoResolution();
switch (maxRes) {
case 0:
return 400000;
@ -665,7 +665,7 @@ public class Preview extends ViewGroup
mRecorder.setProfile(cpHigh);
if (Utils.Companion.needsStupidWritePermissions(getContext(), mCurrVideoPath)) {
final Config config = Config.newInstance(getContext());
final Config config = Config.Companion.newInstance(getContext());
if (config.getTreeUri().isEmpty()) {
Utils.Companion.showToast(mContext, R.string.save_error_internal_storage);
config.setSavePhotosFolder(Environment.getExternalStorageDirectory().toString());
@ -762,7 +762,7 @@ public class Preview extends ViewGroup
}
private void toggleShutterSound(Boolean mute) {
if (!Config.newInstance(mContext).getIsSoundEnabled()) {
if (!Config.Companion.newInstance(mContext).isSoundEnabled()) {
((AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE)).setStreamMute(AudioManager.STREAM_SYSTEM, mute);
}
}

View File

@ -699,7 +699,7 @@ public class MainActivity extends SimpleActivity
@Override
protected void onDestroy() {
super.onDestroy();
Config.newInstance(getApplicationContext()).setIsFirstRun(false);
Config.Companion.newInstance(getApplicationContext()).setFirstRun(false);
if (mPreview != null)
mPreview.releaseCamera();
}

View File

@ -0,0 +1,78 @@
package com.simplemobiletools.camera
import android.content.Context
import android.content.SharedPreferences
import android.hardware.Camera
import android.os.Environment
class Config(context: Context) {
private val mPrefs: SharedPreferences
companion object {
fun newInstance(context: Context) = Config(context)
}
init {
mPrefs = context.getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE)
}
var isFirstRun: Boolean
get() = mPrefs.getBoolean(Constants.IS_FIRST_RUN, true)
set(firstRun) = mPrefs.edit().putBoolean(Constants.IS_FIRST_RUN, firstRun).apply()
var isDarkTheme: Boolean
get() = mPrefs.getBoolean(Constants.IS_DARK_THEME, false)
set(isDarkTheme) = mPrefs.edit().putBoolean(Constants.IS_DARK_THEME, isDarkTheme).apply()
var savePhotosFolder: String
get() = mPrefs.getString(Constants.SAVE_PHOTOS, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString())
set(path) = mPrefs.edit().putString(Constants.SAVE_PHOTOS, path).apply()
var forceRatioEnabled: Boolean
get() = mPrefs.getBoolean(Constants.FORCE_RATIO, true)
set(enabled) = mPrefs.edit().putBoolean(Constants.FORCE_RATIO, enabled).apply()
// todo: delete this
val maxResolution: Int
get() = mPrefs.getInt(Constants.MAX_RESOLUTION, -1)
var maxPhotoResolution: Int
get() = mPrefs.getInt(Constants.MAX_PHOTO_RESOLUTION, oldDefaultResolution)
set(maxRes) = mPrefs.edit().putInt(Constants.MAX_PHOTO_RESOLUTION, maxRes).apply()
private val oldDefaultResolution: Int
get() {
return when (maxResolution) {
1 -> Constants.EIGHT_MPX
2 -> 0
else -> Constants.FIVE_MPX
}
}
var maxVideoResolution: Int
get() {
val maxRes = mPrefs.getInt(Constants.MAX_VIDEO_RESOLUTION, Constants.P720)
return when (maxRes) {
0 -> Constants.P480
2 -> Constants.P1080
else -> Constants.P720
}
}
set(maxRes) = mPrefs.edit().putInt(Constants.MAX_VIDEO_RESOLUTION, maxRes).apply()
var isSoundEnabled: Boolean
get() = mPrefs.getBoolean(Constants.SOUND, true)
set(enabled) = mPrefs.edit().putBoolean(Constants.SOUND, enabled).apply()
var lastUsedCamera: Int
get() = mPrefs.getInt(Constants.LAST_USED_CAMERA, Camera.CameraInfo.CAMERA_FACING_BACK)
set(cameraId) = mPrefs.edit().putInt(Constants.LAST_USED_CAMERA, cameraId).apply()
var lastFlashlightState: Boolean
get() = mPrefs.getBoolean(Constants.LAST_FLASHLIGHT_STATE, false)
set(enabled) = mPrefs.edit().putBoolean(Constants.LAST_FLASHLIGHT_STATE, enabled).apply()
var treeUri: String
get() = mPrefs.getString(Constants.TREE_URI, "")
set(uri) = mPrefs.edit().putString(Constants.TREE_URI, uri).apply()
}