convert Constants to kotlin

This commit is contained in:
tibbi 2016-11-13 23:13:41 +01:00
parent 4a2ecc15a0
commit 930150416c
10 changed files with 94 additions and 96 deletions

View file

@ -1,33 +0,0 @@
package com.simplemobiletools.gallery;
public class Constants {
public static final String DIRECTORY = "directory";
public static final String MEDIUM = "medium";
public static final String GET_IMAGE_INTENT = "get_image_intent";
public static final String GET_VIDEO_INTENT = "get_video_intent";
public static final String GET_ANY_INTENT = "get_any_intent";
public static final String SET_WALLPAPER_INTENT = "set_wallpaper_intent";
// shared preferences
public static final String PREFS_KEY = "Gallery";
public static final String IS_FIRST_RUN = "is_first_run";
public static final String IS_DARK_THEME = "is_dark_theme";
public static final String IS_SAME_SORTING = "is_same_sorting";
public static final String SORT_ORDER = "sort_order";
public static final String DIRECTORY_SORT_ORDER = "directory_sort_order";
public static final String HIDDEN_FOLDERS = "hidden_folders";
public static final String SHOW_HIDDEN_FOLDERS = "show_hidden_folders";
public static final String AUTOPLAY_VIDEOS = "autoplay_videos";
public static final String TREE_URI = "tree_uri";
public static final String DISPLAY_FILE_NAMES = "display_file_names";
// sorting
public static final int SORT_BY_NAME = 1;
public static final int SORT_BY_DATE = 2;
public static final int SORT_BY_SIZE = 4;
public static final int SORT_DESCENDING = 1024;
// global intents
public static final int OPEN_DOCUMENT_TREE = 1000;
}

View file

@ -444,15 +444,15 @@ public class MainActivity extends SimpleActivity
@Override @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final Intent intent = new Intent(this, MediaActivity.class); final Intent intent = new Intent(this, MediaActivity.class);
intent.putExtra(Constants.DIRECTORY, mDirs.get(position).getPath()); intent.putExtra(Constants.INSTANCE.getDIRECTORY(), mDirs.get(position).getPath());
if (mIsSetWallpaperIntent) { if (mIsSetWallpaperIntent) {
intent.putExtra(Constants.SET_WALLPAPER_INTENT, true); intent.putExtra(Constants.INSTANCE.getSET_WALLPAPER_INTENT(), true);
startActivityForResult(intent, PICK_WALLPAPER); startActivityForResult(intent, PICK_WALLPAPER);
} else { } else {
intent.putExtra(Constants.GET_IMAGE_INTENT, mIsPickImageIntent || mIsGetImageContentIntent); intent.putExtra(Constants.INSTANCE.getGET_IMAGE_INTENT(), mIsPickImageIntent || mIsGetImageContentIntent);
intent.putExtra(Constants.GET_VIDEO_INTENT, mIsPickVideoIntent || mIsGetVideoContentIntent); intent.putExtra(Constants.INSTANCE.getGET_VIDEO_INTENT(), mIsPickVideoIntent || mIsGetVideoContentIntent);
intent.putExtra(Constants.GET_ANY_INTENT, mIsGetAnyContentIntent); intent.putExtra(Constants.INSTANCE.getGET_ANY_INTENT(), mIsGetAnyContentIntent);
startActivityForResult(intent, PICK_MEDIA); startActivityForResult(intent, PICK_MEDIA);
} }
} }

View file

@ -73,12 +73,12 @@ public class MediaActivity extends SimpleActivity
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_media); setContentView(R.layout.activity_media);
ButterKnife.bind(this); ButterKnife.bind(this);
mIsGetImageIntent = getIntent().getBooleanExtra(Constants.GET_IMAGE_INTENT, false); mIsGetImageIntent = getIntent().getBooleanExtra(Constants.INSTANCE.getGET_IMAGE_INTENT(), false);
mIsGetVideoIntent = getIntent().getBooleanExtra(Constants.GET_VIDEO_INTENT, false); mIsGetVideoIntent = getIntent().getBooleanExtra(Constants.INSTANCE.getGET_VIDEO_INTENT(), false);
mIsGetAnyIntent = getIntent().getBooleanExtra(Constants.GET_ANY_INTENT, false); mIsGetAnyIntent = getIntent().getBooleanExtra(Constants.INSTANCE.getGET_ANY_INTENT(), false);
mToBeDeleted = new ArrayList<>(); mToBeDeleted = new ArrayList<>();
mSwipeRefreshLayout.setOnRefreshListener(this); mSwipeRefreshLayout.setOnRefreshListener(this);
mPath = getIntent().getStringExtra(Constants.DIRECTORY); mPath = getIntent().getStringExtra(Constants.INSTANCE.getDIRECTORY());
mMedia = new ArrayList<>(); mMedia = new ArrayList<>();
} }
@ -407,7 +407,7 @@ public class MediaActivity extends SimpleActivity
} }
private boolean isSetWallpaperIntent() { private boolean isSetWallpaperIntent() {
return getIntent().getBooleanExtra(Constants.SET_WALLPAPER_INTENT, false); return getIntent().getBooleanExtra(Constants.INSTANCE.getSET_WALLPAPER_INTENT(), false);
} }
private void displayCopyDialog() { private void displayCopyDialog() {
@ -475,7 +475,7 @@ public class MediaActivity extends SimpleActivity
finish(); finish();
} else { } else {
final Intent intent = new Intent(this, ViewPagerActivity.class); final Intent intent = new Intent(this, ViewPagerActivity.class);
intent.putExtra(Constants.MEDIUM, curItemPath); intent.putExtra(Constants.INSTANCE.getMEDIUM(), curItemPath);
startActivity(intent); startActivity(intent);
} }
} }

View file

@ -14,73 +14,73 @@ class Config private constructor(context: Context) {
} }
init { init {
mPrefs = context.getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE) mPrefs = context.getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE)
} }
var isFirstRun: Boolean var isFirstRun: Boolean
get() = mPrefs.getBoolean(Constants.IS_FIRST_RUN, true) get() = mPrefs.getBoolean(IS_FIRST_RUN, true)
set(isFirstRun) = mPrefs.edit().putBoolean(Constants.IS_FIRST_RUN, isFirstRun).apply() set(isFirstRun) = mPrefs.edit().putBoolean(IS_FIRST_RUN, isFirstRun).apply()
var isDarkTheme: Boolean var isDarkTheme: Boolean
get() = mPrefs.getBoolean(Constants.IS_DARK_THEME, true) get() = mPrefs.getBoolean(IS_DARK_THEME, true)
set(isDarkTheme) = mPrefs.edit().putBoolean(Constants.IS_DARK_THEME, isDarkTheme).apply() set(isDarkTheme) = mPrefs.edit().putBoolean(IS_DARK_THEME, isDarkTheme).apply()
var isSameSorting: Boolean var isSameSorting: Boolean
get() = mPrefs.getBoolean(Constants.IS_SAME_SORTING, true) get() = mPrefs.getBoolean(IS_SAME_SORTING, true)
set(isSameSorting) = mPrefs.edit().putBoolean(Constants.IS_SAME_SORTING, isSameSorting).apply() set(isSameSorting) = mPrefs.edit().putBoolean(IS_SAME_SORTING, isSameSorting).apply()
var sorting: Int var sorting: Int
get() = if (isSameSorting) directorySorting else mPrefs.getInt(Constants.SORT_ORDER, Constants.SORT_BY_DATE or Constants.SORT_DESCENDING) get() = if (isSameSorting) directorySorting else mPrefs.getInt(SORT_ORDER, SORT_BY_DATE or SORT_DESCENDING)
set(order) = if (isSameSorting) directorySorting = order else mPrefs.edit().putInt(Constants.SORT_ORDER, order).apply() set(order) = if (isSameSorting) directorySorting = order else mPrefs.edit().putInt(SORT_ORDER, order).apply()
var directorySorting: Int var directorySorting: Int
get() = mPrefs.getInt(Constants.DIRECTORY_SORT_ORDER, Constants.SORT_BY_DATE or Constants.SORT_DESCENDING) get() = mPrefs.getInt(DIRECTORY_SORT_ORDER, SORT_BY_DATE or SORT_DESCENDING)
set(order) = mPrefs.edit().putInt(Constants.DIRECTORY_SORT_ORDER, order).apply() set(order) = mPrefs.edit().putInt(DIRECTORY_SORT_ORDER, order).apply()
var showHiddenFolders: Boolean var showHiddenFolders: Boolean
get() = mPrefs.getBoolean(Constants.SHOW_HIDDEN_FOLDERS, false) get() = mPrefs.getBoolean(SHOW_HIDDEN_FOLDERS, false)
set(showHiddenFolders) = mPrefs.edit().putBoolean(Constants.SHOW_HIDDEN_FOLDERS, showHiddenFolders).apply() set(showHiddenFolders) = mPrefs.edit().putBoolean(SHOW_HIDDEN_FOLDERS, showHiddenFolders).apply()
fun addHiddenDirectory(path: String) { fun addHiddenDirectory(path: String) {
val hiddenFolders = hiddenFolders val hiddenFolders = hiddenFolders
hiddenFolders.add(path) hiddenFolders.add(path)
mPrefs.edit().putStringSet(Constants.HIDDEN_FOLDERS, hiddenFolders).apply() mPrefs.edit().putStringSet(HIDDEN_FOLDERS, hiddenFolders).apply()
} }
fun addHiddenDirectories(paths: Set<String>) { fun addHiddenDirectories(paths: Set<String>) {
val hiddenFolders = hiddenFolders val hiddenFolders = hiddenFolders
hiddenFolders.addAll(paths) hiddenFolders.addAll(paths)
mPrefs.edit().putStringSet(Constants.HIDDEN_FOLDERS, hiddenFolders).apply() mPrefs.edit().putStringSet(HIDDEN_FOLDERS, hiddenFolders).apply()
} }
fun removeHiddenDirectory(path: String) { fun removeHiddenDirectory(path: String) {
val hiddenFolders = hiddenFolders val hiddenFolders = hiddenFolders
hiddenFolders.remove(path) hiddenFolders.remove(path)
mPrefs.edit().putStringSet(Constants.HIDDEN_FOLDERS, hiddenFolders).apply() mPrefs.edit().putStringSet(HIDDEN_FOLDERS, hiddenFolders).apply()
} }
fun removeHiddenDirectories(paths: Set<String>) { fun removeHiddenDirectories(paths: Set<String>) {
val hiddenFolders = hiddenFolders val hiddenFolders = hiddenFolders
hiddenFolders.removeAll(paths) hiddenFolders.removeAll(paths)
mPrefs.edit().putStringSet(Constants.HIDDEN_FOLDERS, hiddenFolders).apply() mPrefs.edit().putStringSet(HIDDEN_FOLDERS, hiddenFolders).apply()
} }
val hiddenFolders: MutableSet<String> val hiddenFolders: MutableSet<String>
get() = mPrefs.getStringSet(Constants.HIDDEN_FOLDERS, HashSet<String>()) get() = mPrefs.getStringSet(HIDDEN_FOLDERS, HashSet<String>())
fun getIsFolderHidden(path: String): Boolean { fun getIsFolderHidden(path: String): Boolean {
return hiddenFolders.contains(path) return hiddenFolders.contains(path)
} }
var autoplayVideos: Boolean var autoplayVideos: Boolean
get() = mPrefs.getBoolean(Constants.AUTOPLAY_VIDEOS, false) get() = mPrefs.getBoolean(AUTOPLAY_VIDEOS, false)
set(autoplay) = mPrefs.edit().putBoolean(Constants.AUTOPLAY_VIDEOS, autoplay).apply() set(autoplay) = mPrefs.edit().putBoolean(AUTOPLAY_VIDEOS, autoplay).apply()
var treeUri: String var treeUri: String
get() = mPrefs.getString(Constants.TREE_URI, "") get() = mPrefs.getString(TREE_URI, "")
set(uri) = mPrefs.edit().putString(Constants.TREE_URI, uri).apply() set(uri) = mPrefs.edit().putString(TREE_URI, uri).apply()
var displayFileNames: Boolean var displayFileNames: Boolean
get() = mPrefs.getBoolean(Constants.DISPLAY_FILE_NAMES, false) get() = mPrefs.getBoolean(DISPLAY_FILE_NAMES, false)
set(display) = mPrefs.edit().putBoolean(Constants.DISPLAY_FILE_NAMES, display).apply() set(display) = mPrefs.edit().putBoolean(DISPLAY_FILE_NAMES, display).apply()
} }

View file

@ -0,0 +1,33 @@
package com.simplemobiletools.gallery
// shared preferences
val PREFS_KEY = "Gallery"
val IS_FIRST_RUN = "is_first_run"
val IS_DARK_THEME = "is_dark_theme"
val IS_SAME_SORTING = "is_same_sorting"
val SORT_ORDER = "sort_order"
val DIRECTORY_SORT_ORDER = "directory_sort_order"
val HIDDEN_FOLDERS = "hidden_folders"
val SHOW_HIDDEN_FOLDERS = "show_hidden_folders"
val AUTOPLAY_VIDEOS = "autoplay_videos"
val TREE_URI = "tree_uri"
val DISPLAY_FILE_NAMES = "display_file_names"
// sorting
val SORT_BY_NAME = 1
val SORT_BY_DATE = 2
val SORT_BY_SIZE = 4
val SORT_DESCENDING = 1024
// global intents
val OPEN_DOCUMENT_TREE = 1000
object Constants {
val DIRECTORY = "directory"
val MEDIUM = "medium"
val GET_IMAGE_INTENT = "get_image_intent"
val GET_VIDEO_INTENT = "get_video_intent"
val GET_ANY_INTENT = "get_any_intent"
val SET_WALLPAPER_INTENT = "set_wallpaper_intent"
}

View file

@ -9,7 +9,7 @@ import android.support.v7.app.AppCompatActivity
import android.view.MenuItem import android.view.MenuItem
import com.simplemobiletools.filepicker.extensions.isShowingWritePermissions import com.simplemobiletools.filepicker.extensions.isShowingWritePermissions
import com.simplemobiletools.gallery.Config import com.simplemobiletools.gallery.Config
import com.simplemobiletools.gallery.Constants import com.simplemobiletools.gallery.OPEN_DOCUMENT_TREE
import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.extensions.hideSystemUI import com.simplemobiletools.gallery.extensions.hideSystemUI
import com.simplemobiletools.gallery.extensions.showSystemUI import com.simplemobiletools.gallery.extensions.showSystemUI
@ -40,7 +40,7 @@ open class SimpleActivity : AppCompatActivity() {
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) { override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
super.onActivityResult(requestCode, resultCode, resultData) super.onActivityResult(requestCode, resultCode, resultData)
if (requestCode == Constants.OPEN_DOCUMENT_TREE && resultCode == Activity.RESULT_OK && resultData != null) { if (requestCode == OPEN_DOCUMENT_TREE && resultCode == Activity.RESULT_OK && resultData != null) {
saveTreeUri(resultData) saveTreeUri(resultData)
} }
} }
@ -54,7 +54,7 @@ open class SimpleActivity : AppCompatActivity() {
contentResolver.takePersistableUriPermission(treeUri, takeFlags) contentResolver.takePersistableUriPermission(treeUri, takeFlags)
} }
fun isShowingPermDialog(file: File) = isShowingWritePermissions(file, mConfig.treeUri, Constants.OPEN_DOCUMENT_TREE) fun isShowingPermDialog(file: File) = isShowingWritePermissions(file, mConfig.treeUri, OPEN_DOCUMENT_TREE)
fun hideUI() = hideSystemUI(supportActionBar, window) fun hideUI() = hideSystemUI(supportActionBar, window)

View file

@ -4,10 +4,7 @@ import android.content.Context
import android.os.AsyncTask import android.os.AsyncTask
import android.provider.MediaStore import android.provider.MediaStore
import com.simplemobiletools.filepicker.extensions.scanFiles import com.simplemobiletools.filepicker.extensions.scanFiles
import com.simplemobiletools.gallery.Config import com.simplemobiletools.gallery.*
import com.simplemobiletools.gallery.Constants
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.Utils
import com.simplemobiletools.gallery.models.Directory import com.simplemobiletools.gallery.models.Directory
import java.io.File import java.io.File
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
@ -95,11 +92,11 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
private fun getSortOrder(): String { private fun getSortOrder(): String {
val sorting = mConfig.directorySorting val sorting = mConfig.directorySorting
var sortBy = MediaStore.Images.Media.DATE_MODIFIED var sortBy = MediaStore.Images.Media.DATE_MODIFIED
if (sorting and Constants.SORT_BY_NAME != 0) { if (sorting and SORT_BY_NAME != 0) {
sortBy = MediaStore.Images.Media.DATA sortBy = MediaStore.Images.Media.DATA
} }
if (sorting and Constants.SORT_DESCENDING != 0) { if (sorting and SORT_DESCENDING != 0) {
sortBy += " DESC" sortBy += " DESC"
} }
return sortBy return sortBy

View file

@ -5,9 +5,7 @@ import android.content.DialogInterface
import android.support.v7.app.AlertDialog import android.support.v7.app.AlertDialog
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import com.simplemobiletools.gallery.Config import com.simplemobiletools.gallery.*
import com.simplemobiletools.gallery.Constants
import com.simplemobiletools.gallery.R
import kotlinx.android.synthetic.main.dialog_change_sorting.view.* import kotlinx.android.synthetic.main.dialog_change_sorting.view.*
class ChangeSortingDialog(val activity: Activity, val isDirectorySorting: Boolean, val listener: OnChangeSortingListener) : DialogInterface.OnClickListener { class ChangeSortingDialog(val activity: Activity, val isDirectorySorting: Boolean, val listener: OnChangeSortingListener) : DialogInterface.OnClickListener {
@ -39,9 +37,9 @@ class ChangeSortingDialog(val activity: Activity, val isDirectorySorting: Boolea
val sortingRadio = view.dialog_radio_sorting val sortingRadio = view.dialog_radio_sorting
var sortBtn = sortingRadio.dialog_radio_name var sortBtn = sortingRadio.dialog_radio_name
if (currSorting and Constants.SORT_BY_DATE != 0) { if (currSorting and SORT_BY_DATE != 0) {
sortBtn = sortingRadio.dialog_radio_date sortBtn = sortingRadio.dialog_radio_date
} else if (currSorting and Constants.SORT_BY_SIZE != 0) { } else if (currSorting and SORT_BY_SIZE != 0) {
sortBtn = sortingRadio.dialog_radio_size sortBtn = sortingRadio.dialog_radio_size
} }
sortBtn.isChecked = true sortBtn.isChecked = true
@ -51,7 +49,7 @@ class ChangeSortingDialog(val activity: Activity, val isDirectorySorting: Boolea
val orderRadio = view.dialog_radio_order val orderRadio = view.dialog_radio_order
var orderBtn = orderRadio.dialog_radio_ascending var orderBtn = orderRadio.dialog_radio_ascending
if (currSorting and Constants.SORT_DESCENDING != 0) { if (currSorting and SORT_DESCENDING != 0) {
orderBtn = orderRadio.dialog_radio_descending orderBtn = orderRadio.dialog_radio_descending
} }
orderBtn.isChecked = true orderBtn.isChecked = true
@ -60,13 +58,13 @@ class ChangeSortingDialog(val activity: Activity, val isDirectorySorting: Boolea
override fun onClick(dialog: DialogInterface, which: Int) { override fun onClick(dialog: DialogInterface, which: Int) {
val sortingRadio = view.dialog_radio_sorting val sortingRadio = view.dialog_radio_sorting
var sorting = when (sortingRadio.checkedRadioButtonId) { var sorting = when (sortingRadio.checkedRadioButtonId) {
R.id.dialog_radio_name -> Constants.SORT_BY_NAME R.id.dialog_radio_name -> SORT_BY_NAME
R.id.dialog_radio_date -> Constants.SORT_BY_DATE R.id.dialog_radio_date -> SORT_BY_DATE
else -> Constants.SORT_BY_SIZE else -> SORT_BY_SIZE
} }
if (view.dialog_radio_order.checkedRadioButtonId == R.id.dialog_radio_descending) { if (view.dialog_radio_order.checkedRadioButtonId == R.id.dialog_radio_descending) {
sorting = sorting or Constants.SORT_DESCENDING sorting = sorting or SORT_DESCENDING
} }
if (isDirectorySorting) { if (isDirectorySorting) {

View file

@ -1,6 +1,8 @@
package com.simplemobiletools.gallery.models package com.simplemobiletools.gallery.models
import com.simplemobiletools.gallery.Constants import com.simplemobiletools.gallery.SORT_BY_DATE
import com.simplemobiletools.gallery.SORT_BY_NAME
import com.simplemobiletools.gallery.SORT_DESCENDING
class Directory(val path: String, val thumbnail: String, val name: String, var mediaCnt: Int, val timestamp: Long, var size: Long) : Comparable<Directory> { class Directory(val path: String, val thumbnail: String, val name: String, var mediaCnt: Int, val timestamp: Long, var size: Long) : Comparable<Directory> {
fun addSize(bytes: Long) { fun addSize(bytes: Long) {
@ -9,15 +11,15 @@ class Directory(val path: String, val thumbnail: String, val name: String, var m
override fun compareTo(other: Directory): Int { override fun compareTo(other: Directory): Int {
var res: Int var res: Int
if (sorting and Constants.SORT_BY_NAME != 0) { if (sorting and SORT_BY_NAME != 0) {
res = path.compareTo(other.path) res = path.compareTo(other.path)
} else if (sorting and Constants.SORT_BY_DATE != 0) { } else if (sorting and SORT_BY_DATE != 0) {
res = if (timestamp > other.timestamp) 1 else -1 res = if (timestamp > other.timestamp) 1 else -1
} else { } else {
res = if (size > other.size) 1 else -1 res = if (size > other.size) 1 else -1
} }
if (sorting and Constants.SORT_DESCENDING != 0) { if (sorting and SORT_DESCENDING != 0) {
res *= -1 res *= -1
} }
return res return res

View file

@ -1,7 +1,8 @@
package com.simplemobiletools.gallery.models package com.simplemobiletools.gallery.models
import com.simplemobiletools.gallery.Constants import com.simplemobiletools.gallery.SORT_BY_DATE
import com.simplemobiletools.gallery.SORT_BY_NAME
import com.simplemobiletools.gallery.SORT_DESCENDING
import java.io.Serializable import java.io.Serializable
class Medium(val name: String, var path: String, val isVideo: Boolean, val timestamp: Long, val size: Long) : Serializable, Comparable<Medium> { class Medium(val name: String, var path: String, val isVideo: Boolean, val timestamp: Long, val size: Long) : Serializable, Comparable<Medium> {
@ -15,15 +16,15 @@ class Medium(val name: String, var path: String, val isVideo: Boolean, val times
override fun compareTo(other: Medium): Int { override fun compareTo(other: Medium): Int {
var res: Int var res: Int
if (sorting and Constants.SORT_BY_NAME != 0) { if (sorting and SORT_BY_NAME != 0) {
res = path.compareTo(other.path) res = path.compareTo(other.path)
} else if (sorting and Constants.SORT_BY_DATE != 0) { } else if (sorting and SORT_BY_DATE != 0) {
res = if (timestamp > other.timestamp) 1 else -1 res = if (timestamp > other.timestamp) 1 else -1
} else { } else {
res = if (size > other.size) 1 else -1 res = if (size > other.size) 1 else -1
} }
if (sorting and Constants.SORT_DESCENDING != 0) { if (sorting and SORT_DESCENDING != 0) {
res *= -1 res *= -1
} }
return res return res