add an initial version of the Commons library

This commit is contained in:
tibbi 2017-03-19 13:01:08 +01:00
parent c05dcff4e6
commit 1ef1bbe176
11 changed files with 55 additions and 156 deletions

View File

@ -32,10 +32,7 @@ android {
} }
dependencies { dependencies {
compile 'com.android.support:appcompat-v7:25.3.0' compile 'com.simplemobiletools:commons:2.13.5'
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 "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
} }

View File

@ -1,62 +1,44 @@
package com.simplemobiletools.camera package com.simplemobiletools.camera
import android.content.Context import android.content.Context
import android.content.SharedPreferences
import android.hardware.Camera import android.hardware.Camera
import android.os.Environment import android.os.Environment
import com.simplemobiletools.commons.helpers.BaseConfig
class Config(context: Context) { class Config(context: Context) : BaseConfig(context) {
private val mPrefs: SharedPreferences
companion object { companion object {
fun newInstance(context: Context) = Config(context) 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 var savePhotosFolder: String
get() = mPrefs.getString(SAVE_PHOTOS, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString()) get() = prefs.getString(SAVE_PHOTOS, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString())
set(path) = mPrefs.edit().putString(SAVE_PHOTOS, path).apply() set(path) = prefs.edit().putString(SAVE_PHOTOS, path).apply()
var isShowPreviewEnabled: Boolean var isShowPreviewEnabled: Boolean
get() = mPrefs.getBoolean(SHOW_PREVIEW, false) get() = prefs.getBoolean(SHOW_PREVIEW, false)
set(enabled) = mPrefs.edit().putBoolean(SHOW_PREVIEW, enabled).apply() set(enabled) = prefs.edit().putBoolean(SHOW_PREVIEW, enabled).apply()
var forceRatioEnabled: Boolean var forceRatioEnabled: Boolean
get() = mPrefs.getBoolean(FORCE_RATIO, true) get() = prefs.getBoolean(FORCE_RATIO, true)
set(enabled) = mPrefs.edit().putBoolean(FORCE_RATIO, enabled).apply() set(enabled) = prefs.edit().putBoolean(FORCE_RATIO, enabled).apply()
var maxPhotoResolution: Int var maxPhotoResolution: Int
get() = mPrefs.getInt(MAX_PHOTO_RESOLUTION, FIVE_MPX) get() = prefs.getInt(MAX_PHOTO_RESOLUTION, FIVE_MPX)
set(maxRes) = mPrefs.edit().putInt(MAX_PHOTO_RESOLUTION, maxRes).apply() set(maxRes) = prefs.edit().putInt(MAX_PHOTO_RESOLUTION, maxRes).apply()
var maxVideoResolution: Int var maxVideoResolution: Int
get() = mPrefs.getInt(MAX_VIDEO_RESOLUTION, P720) get() = prefs.getInt(MAX_VIDEO_RESOLUTION, P720)
set(maxRes) = mPrefs.edit().putInt(MAX_VIDEO_RESOLUTION, maxRes).apply() set(maxRes) = prefs.edit().putInt(MAX_VIDEO_RESOLUTION, maxRes).apply()
var isSoundEnabled: Boolean var isSoundEnabled: Boolean
get() = mPrefs.getBoolean(SOUND, true) get() = prefs.getBoolean(SOUND, true)
set(enabled) = mPrefs.edit().putBoolean(SOUND, enabled).apply() set(enabled) = prefs.edit().putBoolean(SOUND, enabled).apply()
var lastUsedCamera: Int var lastUsedCamera: Int
get() = mPrefs.getInt(LAST_USED_CAMERA, Camera.CameraInfo.CAMERA_FACING_BACK) get() = prefs.getInt(LAST_USED_CAMERA, Camera.CameraInfo.CAMERA_FACING_BACK)
set(cameraId) = mPrefs.edit().putInt(LAST_USED_CAMERA, cameraId).apply() set(cameraId) = prefs.edit().putInt(LAST_USED_CAMERA, cameraId).apply()
var lastFlashlightState: Boolean var lastFlashlightState: Boolean
get() = mPrefs.getBoolean(LAST_FLASHLIGHT_STATE, false) get() = prefs.getBoolean(LAST_FLASHLIGHT_STATE, false)
set(enabled) = mPrefs.edit().putBoolean(LAST_FLASHLIGHT_STATE, enabled).apply() set(enabled) = prefs.edit().putBoolean(LAST_FLASHLIGHT_STATE, enabled).apply()
var treeUri: String
get() = mPrefs.getString(TREE_URI, "")
set(uri) = mPrefs.edit().putString(TREE_URI, uri).apply()
} }

View File

@ -15,9 +15,6 @@ val P720 = 1000000
val P1080 = 2100000 val P1080 = 2100000
// shared preferences // 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 SAVE_PHOTOS = "save_photos"
val SHOW_PREVIEW = "show_preview" val SHOW_PREVIEW = "show_preview"
val SOUND = "sound" val SOUND = "sound"
@ -26,4 +23,3 @@ val MAX_PHOTO_RESOLUTION = "max_photo_resolution"
val MAX_VIDEO_RESOLUTION = "max_video_resolution" val MAX_VIDEO_RESOLUTION = "max_video_resolution"
val LAST_USED_CAMERA = "last_used_camera" val LAST_USED_CAMERA = "last_used_camera"
val LAST_FLASHLIGHT_STATE = "last_flashlight_state" val LAST_FLASHLIGHT_STATE = "last_flashlight_state"
val TREE_URI = "tree_uri"

View File

@ -5,9 +5,9 @@ import android.os.AsyncTask
import android.os.Environment import android.os.Environment
import android.util.Log import android.util.Log
import com.simplemobiletools.camera.activities.MainActivity import com.simplemobiletools.camera.activities.MainActivity
import com.simplemobiletools.filepicker.extensions.getFileDocument import com.simplemobiletools.commons.extensions.getFileDocument
import com.simplemobiletools.filepicker.extensions.needsStupidWritePermissions import com.simplemobiletools.commons.extensions.needsStupidWritePermissions
import com.simplemobiletools.filepicker.extensions.toast import com.simplemobiletools.commons.extensions.toast
import java.io.* import java.io.*
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
@ -46,8 +46,8 @@ class PhotoProcessor(val activity: MainActivity, val uri: Uri?) : AsyncTask<Byte
return "" return ""
} }
var document = activity.getFileDocument(path, config.treeUri) var document = activity.getFileDocument(path, config.treeUri)
document = document.createFile("", path.substring(path.lastIndexOf('/') + 1)) document = document?.createFile("", path.substring(path.lastIndexOf('/') + 1))
fos = activity.contentResolver.openOutputStream(document.uri) fos = activity.contentResolver.openOutputStream(document?.uri)
} else { } else {
fos = FileOutputStream(photoFile) fos = FileOutputStream(photoFile)
} }

View File

@ -9,9 +9,9 @@ import android.graphics.Point
import android.hardware.Camera import android.hardware.Camera
import android.support.v4.content.ContextCompat import android.support.v4.content.ContextCompat
import android.view.Surface import android.view.Surface
import com.simplemobiletools.filepicker.extensions.getFileDocument import com.simplemobiletools.commons.extensions.getFileDocument
import com.simplemobiletools.filepicker.extensions.needsStupidWritePermissions import com.simplemobiletools.commons.extensions.needsStupidWritePermissions
import com.simplemobiletools.filepicker.extensions.toast import com.simplemobiletools.commons.extensions.toast
import java.io.File import java.io.File
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.* import java.util.*

View File

@ -1,12 +1,10 @@
package com.simplemobiletools.camera.activities package com.simplemobiletools.camera.activities
import android.content.ActivityNotFoundException
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.text.Html import android.text.Html
import android.text.method.LinkMovementMethod import android.text.method.LinkMovementMethod
import android.view.View
import com.simplemobiletools.camera.BuildConfig import com.simplemobiletools.camera.BuildConfig
import com.simplemobiletools.camera.R import com.simplemobiletools.camera.R
import kotlinx.android.synthetic.main.activity_about.* import kotlinx.android.synthetic.main.activity_about.*
@ -43,7 +41,7 @@ class AboutActivity : SimpleActivity() {
} }
private fun setupRateUs() { private fun setupRateUs() {
if (config.isFirstRun) { /*if (config.isFirstRun) {
about_rate_us.visibility = View.GONE about_rate_us.visibility = View.GONE
} else { } else {
about_rate_us.setOnClickListener { about_rate_us.setOnClickListener {
@ -54,7 +52,7 @@ class AboutActivity : SimpleActivity() {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(getStoreUrl()))) startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(getStoreUrl())))
} }
} }
} }*/
} }
fun setupInvite() { fun setupInvite() {

View File

@ -22,9 +22,10 @@ import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.simplemobiletools.camera.* import com.simplemobiletools.camera.*
import com.simplemobiletools.camera.Preview.PreviewListener import com.simplemobiletools.camera.Preview.PreviewListener
import com.simplemobiletools.camera.extensions.config
import com.simplemobiletools.camera.views.FocusRectView import com.simplemobiletools.camera.views.FocusRectView
import com.simplemobiletools.filepicker.extensions.hasStoragePermission import com.simplemobiletools.commons.extensions.hasWriteStoragePermission
import com.simplemobiletools.filepicker.extensions.toast import com.simplemobiletools.commons.extensions.toast
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import java.util.* import java.util.*
@ -97,7 +98,7 @@ class MainActivity : SimpleActivity(), SensorEventListener, PreviewListener, Pho
if (!Utils.hasCameraPermission(applicationContext)) { if (!Utils.hasCameraPermission(applicationContext)) {
permissions.add(Manifest.permission.CAMERA) permissions.add(Manifest.permission.CAMERA)
} }
if (!hasStoragePermission()) { if (!hasWriteStoragePermission()) {
permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE) permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
} }
ActivityCompat.requestPermissions(this, permissions.toTypedArray(), CAMERA_STORAGE_PERMISSION) ActivityCompat.requestPermissions(this, permissions.toTypedArray(), CAMERA_STORAGE_PERMISSION)
@ -156,7 +157,7 @@ class MainActivity : SimpleActivity(), SensorEventListener, PreviewListener, Pho
toggle_photo_video.setOnClickListener { handleTogglePhotoVideo() } toggle_photo_video.setOnClickListener { handleTogglePhotoVideo() }
} }
private fun hasCameraAndStoragePermission() = Utils.hasCameraPermission(applicationContext) && hasStoragePermission() private fun hasCameraAndStoragePermission() = Utils.hasCameraPermission(applicationContext) && hasWriteStoragePermission()
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) { override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults) super.onRequestPermissionsResult(requestCode, permissions, grantResults)

View File

@ -1,22 +1,10 @@
package com.simplemobiletools.camera.activities package com.simplemobiletools.camera.activities
import android.annotation.TargetApi
import android.app.Activity
import android.content.Intent import android.content.Intent
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.support.v4.app.TaskStackBuilder
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View import com.simplemobiletools.camera.R
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
class SettingsActivity : SimpleActivity() { class SettingsActivity : SimpleActivity() {
val OPEN_DOCUMENT_TREE = 1 val OPEN_DOCUMENT_TREE = 1
@ -27,13 +15,12 @@ class SettingsActivity : SimpleActivity() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_settings) setContentView(R.layout.activity_settings)
setupDarkTheme() /*setupSavePhotosFolder()
setupSavePhotosFolder()
setupShowPreview() setupShowPreview()
setupSound() setupSound()
setupForceRatio() setupForceRatio()
setupMaxPhotoResolution() setupMaxPhotoResolution()
setupMaxVideoResolution() setupMaxVideoResolution()*/
} }
override fun onCreateOptionsMenu(menu: Menu): Boolean { override fun onCreateOptionsMenu(menu: Menu): Boolean {
@ -51,37 +38,23 @@ class SettingsActivity : SimpleActivity() {
} }
} }
private fun setupDarkTheme() { /*private fun setupSavePhotosFolder() {
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() {
mCurrPath = config.savePhotosFolder mCurrPath = config.savePhotosFolder
settings_save_photos.text = getHumanPath() settings_save_photos.text = getHumanPath()
settings_save_photos_holder.setOnClickListener { settings_save_photos_holder.setOnClickListener {
FilePickerDialog(this, mCurrPath, false, false, object : FilePickerDialog.OnFilePickerListener { FilePickerDialog(this, mCurrPath, false) {
override fun onFail(error: FilePickerDialog.FilePickerResult) { 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 { private fun getHumanPath(): String {
val basePath = mCurrPath.getBasePath(applicationContext) val basePath = mCurrPath.getBasePath(this)
val path = mCurrPath.replaceFirst(basePath, getStorageName(basePath)).trimEnd('/') val path = mCurrPath.replaceFirst(basePath, getStorageName(basePath)).trimEnd('/')
return if (path.contains('/')) return if (path.contains('/'))
@ -200,9 +173,5 @@ class SettingsActivity : SimpleActivity() {
2 -> P1080 2 -> P1080
else -> -1 else -> -1
} }
} }*/
private fun restartActivity() {
TaskStackBuilder.create(applicationContext).addNextIntentWithParentStack(intent).startActivities()
}
} }

View File

@ -1,32 +1,10 @@
package com.simplemobiletools.camera.activities package com.simplemobiletools.camera.activities
import android.os.Bundle import android.os.Bundle
import android.support.v7.app.AppCompatActivity import com.simplemobiletools.commons.activities.BaseSimpleActivity
import android.view.MenuItem
import com.simplemobiletools.camera.Config
import com.simplemobiletools.camera.R
open class SimpleActivity : AppCompatActivity() {
lateinit var config: Config
open class SimpleActivity : BaseSimpleActivity() {
override fun onCreate(savedInstanceState: Bundle?) { 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) super.onCreate(savedInstanceState)
} }
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
android.R.id.home -> {
finish()
true
}
else -> super.onOptionsItemSelected(item)
}
}
} }

View File

@ -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)

View File

@ -12,34 +12,6 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical"> android:orientation="vertical">
<RelativeLayout
android:id="@+id/settings_dark_theme_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/settings_padding"
android:background="?attr/selectableItemBackground"
android:padding="@dimen/activity_margin">
<TextView
android:id="@+id/settings_dark_theme_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:paddingLeft="@dimen/settings_padding"
android:paddingStart="@dimen/settings_padding"
android:text="@string/dark_theme"/>
<android.support.v7.widget.SwitchCompat
android:id="@+id/settings_dark_theme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:background="@null"
android:clickable="false"/>
</RelativeLayout>
<RelativeLayout <RelativeLayout
android:id="@+id/settings_save_photos_holder" android:id="@+id/settings_save_photos_holder"
android:layout_width="match_parent" android:layout_width="match_parent"