From 1ef1bbe176e46eff0be6596c246d78450269823c Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 19 Mar 2017 13:01:08 +0100 Subject: [PATCH] add an initial version of the Commons library --- app/build.gradle | 5 +- .../com/simplemobiletools/camera/Config.kt | 54 ++++++------------ .../com/simplemobiletools/camera/Constants.kt | 4 -- .../camera/PhotoProcessor.kt | 10 ++-- .../com/simplemobiletools/camera/Utils.kt | 6 +- .../camera/activities/AboutActivity.kt | 6 +- .../camera/activities/MainActivity.kt | 9 +-- .../camera/activities/SettingsActivity.kt | 57 +++++-------------- .../camera/activities/SimpleActivity.kt | 26 +-------- .../camera/extensions/context.kt | 6 ++ app/src/main/res/layout/activity_settings.xml | 28 --------- 11 files changed, 55 insertions(+), 156 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/camera/extensions/context.kt diff --git a/app/build.gradle b/app/build.gradle index 5c45c427..28409b77 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -32,10 +32,7 @@ android { } dependencies { - compile 'com.android.support:appcompat-v7:25.3.0' - compile 'com.android.support:design:25.3.0' - compile 'com.simplemobiletools:filepicker:1.6.1@aar' - compile 'com.github.bumptech.glide:glide:3.7.0' + compile 'com.simplemobiletools:commons:2.13.5' compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" } diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/Config.kt b/app/src/main/kotlin/com/simplemobiletools/camera/Config.kt index 447e7300..e69511b6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/Config.kt @@ -1,62 +1,44 @@ package com.simplemobiletools.camera import android.content.Context -import android.content.SharedPreferences import android.hardware.Camera import android.os.Environment +import com.simplemobiletools.commons.helpers.BaseConfig -class Config(context: Context) { - private val mPrefs: SharedPreferences - +class Config(context: Context) : BaseConfig(context) { companion object { fun newInstance(context: Context) = Config(context) } - init { - mPrefs = context.getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE) - } - - var isFirstRun: Boolean - get() = mPrefs.getBoolean(IS_FIRST_RUN, true) - set(firstRun) = mPrefs.edit().putBoolean(IS_FIRST_RUN, firstRun).apply() - - var isDarkTheme: Boolean - get() = mPrefs.getBoolean(IS_DARK_THEME, false) - set(isDarkTheme) = mPrefs.edit().putBoolean(IS_DARK_THEME, isDarkTheme).apply() - var savePhotosFolder: String - get() = mPrefs.getString(SAVE_PHOTOS, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString()) - set(path) = mPrefs.edit().putString(SAVE_PHOTOS, path).apply() + get() = prefs.getString(SAVE_PHOTOS, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString()) + set(path) = prefs.edit().putString(SAVE_PHOTOS, path).apply() var isShowPreviewEnabled: Boolean - get() = mPrefs.getBoolean(SHOW_PREVIEW, false) - set(enabled) = mPrefs.edit().putBoolean(SHOW_PREVIEW, enabled).apply() + get() = prefs.getBoolean(SHOW_PREVIEW, false) + set(enabled) = prefs.edit().putBoolean(SHOW_PREVIEW, enabled).apply() var forceRatioEnabled: Boolean - get() = mPrefs.getBoolean(FORCE_RATIO, true) - set(enabled) = mPrefs.edit().putBoolean(FORCE_RATIO, enabled).apply() + get() = prefs.getBoolean(FORCE_RATIO, true) + set(enabled) = prefs.edit().putBoolean(FORCE_RATIO, enabled).apply() var maxPhotoResolution: Int - get() = mPrefs.getInt(MAX_PHOTO_RESOLUTION, FIVE_MPX) - set(maxRes) = mPrefs.edit().putInt(MAX_PHOTO_RESOLUTION, maxRes).apply() + get() = prefs.getInt(MAX_PHOTO_RESOLUTION, FIVE_MPX) + set(maxRes) = prefs.edit().putInt(MAX_PHOTO_RESOLUTION, maxRes).apply() var maxVideoResolution: Int - get() = mPrefs.getInt(MAX_VIDEO_RESOLUTION, P720) - set(maxRes) = mPrefs.edit().putInt(MAX_VIDEO_RESOLUTION, maxRes).apply() + get() = prefs.getInt(MAX_VIDEO_RESOLUTION, P720) + set(maxRes) = prefs.edit().putInt(MAX_VIDEO_RESOLUTION, maxRes).apply() var isSoundEnabled: Boolean - get() = mPrefs.getBoolean(SOUND, true) - set(enabled) = mPrefs.edit().putBoolean(SOUND, enabled).apply() + get() = prefs.getBoolean(SOUND, true) + set(enabled) = prefs.edit().putBoolean(SOUND, enabled).apply() var lastUsedCamera: Int - get() = mPrefs.getInt(LAST_USED_CAMERA, Camera.CameraInfo.CAMERA_FACING_BACK) - set(cameraId) = mPrefs.edit().putInt(LAST_USED_CAMERA, cameraId).apply() + get() = prefs.getInt(LAST_USED_CAMERA, Camera.CameraInfo.CAMERA_FACING_BACK) + set(cameraId) = prefs.edit().putInt(LAST_USED_CAMERA, cameraId).apply() var lastFlashlightState: Boolean - get() = mPrefs.getBoolean(LAST_FLASHLIGHT_STATE, false) - set(enabled) = mPrefs.edit().putBoolean(LAST_FLASHLIGHT_STATE, enabled).apply() - - var treeUri: String - get() = mPrefs.getString(TREE_URI, "") - set(uri) = mPrefs.edit().putString(TREE_URI, uri).apply() + get() = prefs.getBoolean(LAST_FLASHLIGHT_STATE, false) + set(enabled) = prefs.edit().putBoolean(LAST_FLASHLIGHT_STATE, enabled).apply() } diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/camera/Constants.kt index b6d5e8cd..347053eb 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/Constants.kt @@ -15,9 +15,6 @@ val P720 = 1000000 val P1080 = 2100000 // shared preferences -val PREFS_KEY = "Camera" -val IS_FIRST_RUN = "is_first_run" -val IS_DARK_THEME = "is_dark_theme" val SAVE_PHOTOS = "save_photos" val SHOW_PREVIEW = "show_preview" val SOUND = "sound" @@ -26,4 +23,3 @@ val MAX_PHOTO_RESOLUTION = "max_photo_resolution" val MAX_VIDEO_RESOLUTION = "max_video_resolution" val LAST_USED_CAMERA = "last_used_camera" val LAST_FLASHLIGHT_STATE = "last_flashlight_state" -val TREE_URI = "tree_uri" diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/PhotoProcessor.kt b/app/src/main/kotlin/com/simplemobiletools/camera/PhotoProcessor.kt index ccfd9cb3..a50cefa2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/PhotoProcessor.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/PhotoProcessor.kt @@ -5,9 +5,9 @@ import android.os.AsyncTask import android.os.Environment import android.util.Log import com.simplemobiletools.camera.activities.MainActivity -import com.simplemobiletools.filepicker.extensions.getFileDocument -import com.simplemobiletools.filepicker.extensions.needsStupidWritePermissions -import com.simplemobiletools.filepicker.extensions.toast +import com.simplemobiletools.commons.extensions.getFileDocument +import com.simplemobiletools.commons.extensions.needsStupidWritePermissions +import com.simplemobiletools.commons.extensions.toast import java.io.* import java.lang.ref.WeakReference @@ -46,8 +46,8 @@ class PhotoProcessor(val activity: MainActivity, val uri: Uri?) : AsyncTask, grantResults: IntArray) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/activities/SettingsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/camera/activities/SettingsActivity.kt index 34517d5f..93d9bfd3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/activities/SettingsActivity.kt @@ -1,22 +1,10 @@ package com.simplemobiletools.camera.activities -import android.annotation.TargetApi -import android.app.Activity import android.content.Intent -import android.os.Build import android.os.Bundle -import android.support.v4.app.TaskStackBuilder import android.view.Menu import android.view.MenuItem -import android.view.View -import android.widget.AdapterView -import com.simplemobiletools.camera.* -import com.simplemobiletools.filepicker.dialogs.FilePickerDialog -import com.simplemobiletools.filepicker.extensions.getBasePath -import com.simplemobiletools.filepicker.extensions.getHumanReadablePath -import com.simplemobiletools.filepicker.extensions.isShowingWritePermissions -import kotlinx.android.synthetic.main.activity_settings.* -import java.io.File +import com.simplemobiletools.camera.R class SettingsActivity : SimpleActivity() { val OPEN_DOCUMENT_TREE = 1 @@ -27,13 +15,12 @@ class SettingsActivity : SimpleActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_settings) - setupDarkTheme() - setupSavePhotosFolder() + /*setupSavePhotosFolder() setupShowPreview() setupSound() setupForceRatio() setupMaxPhotoResolution() - setupMaxVideoResolution() + setupMaxVideoResolution()*/ } override fun onCreateOptionsMenu(menu: Menu): Boolean { @@ -51,37 +38,23 @@ class SettingsActivity : SimpleActivity() { } } - private fun setupDarkTheme() { - settings_dark_theme.isChecked = config.isDarkTheme - settings_dark_theme_holder.setOnClickListener { - settings_dark_theme.toggle() - config.isDarkTheme = settings_dark_theme.isChecked - restartActivity() - } - } - - private fun setupSavePhotosFolder() { + /*private fun setupSavePhotosFolder() { mCurrPath = config.savePhotosFolder settings_save_photos.text = getHumanPath() settings_save_photos_holder.setOnClickListener { - FilePickerDialog(this, mCurrPath, false, false, object : FilePickerDialog.OnFilePickerListener { - override fun onFail(error: FilePickerDialog.FilePickerResult) { + FilePickerDialog(this, mCurrPath, false) { + mWantedPath = pickedPath + if (!isShowingWritePermissions(File(pickedPath), config.treeUri, OPEN_DOCUMENT_TREE)) { + mCurrPath = if (pickedPath.length == 1) pickedPath else pickedPath.trimEnd('/') + config.savePhotosFolder = mCurrPath + settings_save_photos.text = getHumanPath() } - - override fun onSuccess(pickedPath: String) { - mWantedPath = pickedPath - if (!isShowingWritePermissions(File(pickedPath), config.treeUri, OPEN_DOCUMENT_TREE)) { - mCurrPath = if (pickedPath.length == 1) pickedPath else pickedPath.trimEnd('/') - config.savePhotosFolder = mCurrPath - settings_save_photos.text = getHumanPath() - } - } - }) + } } } private fun getHumanPath(): String { - val basePath = mCurrPath.getBasePath(applicationContext) + val basePath = mCurrPath.getBasePath(this) val path = mCurrPath.replaceFirst(basePath, getStorageName(basePath)).trimEnd('/') return if (path.contains('/')) @@ -200,9 +173,5 @@ class SettingsActivity : SimpleActivity() { 2 -> P1080 else -> -1 } - } - - private fun restartActivity() { - TaskStackBuilder.create(applicationContext).addNextIntentWithParentStack(intent).startActivities() - } + }*/ } diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/activities/SimpleActivity.kt b/app/src/main/kotlin/com/simplemobiletools/camera/activities/SimpleActivity.kt index 6bb0e1fb..3ede69f9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/activities/SimpleActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/activities/SimpleActivity.kt @@ -1,32 +1,10 @@ package com.simplemobiletools.camera.activities import android.os.Bundle -import android.support.v7.app.AppCompatActivity -import android.view.MenuItem - -import com.simplemobiletools.camera.Config -import com.simplemobiletools.camera.R - -open class SimpleActivity : AppCompatActivity() { - lateinit var config: Config +import com.simplemobiletools.commons.activities.BaseSimpleActivity +open class SimpleActivity : BaseSimpleActivity() { override fun onCreate(savedInstanceState: Bundle?) { - config = Config.newInstance(applicationContext) - var theme = if (config.isDarkTheme) R.style.AppTheme_Base_Dark else R.style.AppTheme_Base - if (this is MainActivity) { - theme = if (config.isDarkTheme) R.style.FullScreenTheme_Dark else R.style.FullScreenTheme - } - setTheme(theme) super.onCreate(savedInstanceState) } - - override fun onOptionsItemSelected(item: MenuItem): Boolean { - return when (item.itemId) { - android.R.id.home -> { - finish() - true - } - else -> super.onOptionsItemSelected(item) - } - } } diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/extensions/context.kt b/app/src/main/kotlin/com/simplemobiletools/camera/extensions/context.kt new file mode 100644 index 00000000..cac74718 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/camera/extensions/context.kt @@ -0,0 +1,6 @@ +package com.simplemobiletools.camera.extensions + +import android.content.Context +import com.simplemobiletools.camera.Config + +val Context.config: Config get() = Config.newInstance(this) diff --git a/app/src/main/res/layout/activity_settings.xml b/app/src/main/res/layout/activity_settings.xml index e3d65dd0..76ada836 100644 --- a/app/src/main/res/layout/activity_settings.xml +++ b/app/src/main/res/layout/activity_settings.xml @@ -12,34 +12,6 @@ android:layout_height="wrap_content" android:orientation="vertical"> - - - - - - - -