1
0
mirror of https://github.com/etesync/android synced 2024-12-22 22:58:29 +00:00

Kotlin: more kotlin migration.

This commit is contained in:
Tom Hacohen 2019-01-05 11:29:03 +00:00
parent f77063ff1a
commit 4c4c94ca1c
14 changed files with 290 additions and 329 deletions

View File

@ -176,7 +176,7 @@ public class App extends Application {
ServiceDB.OpenHelper serviceDB = new ServiceDB.OpenHelper(this);
String lang = new Settings(serviceDB.getReadableDatabase()).getString(App.FORCE_LANGUAGE, null);
if (lang != null && !lang.equals(DEFAULT_LANGUAGE)) {
LanguageUtils.setLanguage(this, lang);
LanguageUtils.INSTANCE.setLanguage(this, lang);
}
serviceDB.close();
@ -395,7 +395,7 @@ public class App extends Application {
}
if (fromVersion < 10) {
HintManager.setHintSeen(this, AccountsActivity.Companion.getHINT_ACCOUNT_ADD(), true);
HintManager.INSTANCE.setHintSeen(this, AccountsActivity.Companion.getHINT_ACCOUNT_ADD(), true);
}
if (fromVersion < 11) {

View File

@ -142,7 +142,7 @@ public class LocalAddressBook extends AndroidAddressBook implements LocalCollect
public void delete() {
AccountManager accountManager = AccountManager.get(context);
AndroidCompat.removeAccount(accountManager, account);
AndroidCompat.INSTANCE.removeAccount(accountManager, account);
}
public LocalAddressBook(Context context, Account account, ContentProviderClient provider) {

View File

@ -139,7 +139,7 @@ class AppSettingsActivity : BaseActivity() {
}
private fun resetHints() {
HintManager.resetHints(context)
HintManager.resetHints(context!!)
Snackbar.make(view!!, R.string.app_settings_reset_hints_success, Snackbar.LENGTH_LONG).show()
}
@ -188,7 +188,7 @@ class AppSettingsActivity : BaseActivity() {
val value = newValue.toString()
if (value == (preference as ListPreference).value) return@OnPreferenceChangeListener true
LanguageUtils.setLanguage(context, value)
LanguageUtils.setLanguage(context!!, value)
settings.putString(App.FORCE_LANGUAGE, newValue.toString())

View File

@ -148,7 +148,7 @@ class ImportFragment : DialogFragment() {
val uri = data.data
App.log.info("Importing uri = " + uri!!.toString())
try {
importFile = File(com.etesync.syncadapter.utils.FileUtils.getPath(context, uri))
importFile = File(com.etesync.syncadapter.utils.FileUtils.getPath(context!!, uri))
Thread(ImportCalendarsLoader()).start()
} catch (e: Exception) {

View File

@ -1,16 +0,0 @@
package com.etesync.syncadapter.utils;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.os.Build;
public class AndroidCompat {
public static void removeAccount (AccountManager accountManager, Account account) {
if (Build.VERSION.SDK_INT >=
Build.VERSION_CODES.LOLLIPOP_MR1) {
accountManager.removeAccountExplicitly(account);
} else {
accountManager.removeAccount(account, null, null);
}
}
}

View File

@ -0,0 +1,15 @@
package com.etesync.syncadapter.utils
import android.accounts.Account
import android.accounts.AccountManager
import android.os.Build
object AndroidCompat {
fun removeAccount(accountManager: AccountManager, account: Account) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
accountManager.removeAccountExplicitly(account)
} else {
accountManager.removeAccount(account, null, null)
}
}
}

View File

@ -1,158 +0,0 @@
package com.etesync.syncadapter.utils;
import android.annotation.SuppressLint;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import java.io.File;
public class FileUtils {
/**
* Get a file path from a Uri. This will get the the path for Storage Access
* Framework Documents, as well as the _data field for the MediaStore and
* other file-based ContentProviders.
*
* @param context The context.
* @param uri The Uri to query.
* @author paulburke
*/
@SuppressLint("NewApi")
public static String getPath(final Context context, final Uri uri) {
if (uri == null) {
return null;
}
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
// TODO handle non-primary volumes
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[]{
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
String path = getDataColumn(context, uri, null, null);
if (path != null) {
File file = new File(path);
if (!file.canRead()) {
return null;
}
}
return path;
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
/**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
*
* @param context The context.
* @param uri The Uri to query.
* @param selection (Optional) Filter used in the query.
* @param selectionArgs (Optional) Selection arguments used in the query.
* @return The value of the _data column, which is typically a file path.
*/
public static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} catch(Exception e) {
return null;
} finally {
if (cursor != null) {
cursor.close();
}
}
return null;
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is ExternalStorageProvider.
*/
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is DownloadsProvider.
*/
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is MediaProvider.
*/
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
}

View File

@ -0,0 +1,146 @@
package com.etesync.syncadapter.utils
import android.annotation.SuppressLint
import android.content.ContentUris
import android.content.Context
import android.database.Cursor
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.DocumentsContract
import android.provider.MediaStore
import java.io.File
object FileUtils {
/**
* Get a file path from a Uri. This will get the the path for Storage Access
* Framework Documents, as well as the _data field for the MediaStore and
* other file-based ContentProviders.
*
* @param context The context.
* @param uri The Uri to query.
* @author paulburke
*/
@SuppressLint("NewApi")
fun getPath(context: Context, uri: Uri?): String? {
if (uri == null) {
return null
}
val isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
val docId = DocumentsContract.getDocumentId(uri)
val split = docId.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
val type = split[0]
if ("primary".equals(type, ignoreCase = true)) {
return Environment.getExternalStorageDirectory().toString() + "/" + split[1]
}
// TODO handle non-primary volumes
} else if (isDownloadsDocument(uri)) {
val id = DocumentsContract.getDocumentId(uri)
val contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), java.lang.Long.valueOf(id))
return getDataColumn(context, contentUri, null, null)
} else if (isMediaDocument(uri)) {
val docId = DocumentsContract.getDocumentId(uri)
val split = docId.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
val type = split[0]
var contentUri: Uri? = null
if ("image" == type) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
} else if ("video" == type) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI
} else if ("audio" == type) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
}
val selection = "_id=?"
val selectionArgs = arrayOf(split[1])
return getDataColumn(context, contentUri, selection, selectionArgs)
}// MediaProvider
// DownloadsProvider
} else if ("content".equals(uri.scheme!!, ignoreCase = true)) {
val path = getDataColumn(context, uri, null, null)
if (path != null) {
val file = File(path)
if (!file.canRead()) {
return null
}
}
return path
} else if ("file".equals(uri.scheme!!, ignoreCase = true)) {
return uri.path
}// File
// MediaStore (and general)
return null
}
/**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
*
* @param context The context.
* @param uri The Uri to query.
* @param selection (Optional) Filter used in the query.
* @param selectionArgs (Optional) Selection arguments used in the query.
* @return The value of the _data column, which is typically a file path.
*/
fun getDataColumn(context: Context, uri: Uri?, selection: String?,
selectionArgs: Array<String>?): String? {
var cursor: Cursor? = null
val column = "_data"
val projection = arrayOf(column)
try {
cursor = context.contentResolver.query(uri!!, projection, selection, selectionArgs, null)
if (cursor != null && cursor.moveToFirst()) {
val column_index = cursor.getColumnIndexOrThrow(column)
return cursor.getString(column_index)
}
} catch (e: Exception) {
return null
} finally {
cursor?.close()
}
return null
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is ExternalStorageProvider.
*/
fun isExternalStorageDocument(uri: Uri): Boolean {
return "com.android.externalstorage.documents" == uri.authority
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is DownloadsProvider.
*/
fun isDownloadsDocument(uri: Uri): Boolean {
return "com.android.providers.downloads.documents" == uri.authority
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is MediaProvider.
*/
fun isMediaDocument(uri: Uri): Boolean {
return "com.android.providers.media.documents" == uri.authority
}
}

View File

@ -1,34 +0,0 @@
package com.etesync.syncadapter.utils;
import android.content.Context;
import android.content.SharedPreferences;
import java.util.LinkedList;
import java.util.List;
public class HintManager {
private final static String PREF_NAME = "hints";
private static SharedPreferences getPrefs(Context context) {
return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
}
public static void setHintSeen(Context context, String hint, boolean seen) {
SharedPreferences prefs = getPrefs(context);
prefs.edit().putBoolean(hint, seen).apply();
}
public static boolean getHintSeen(Context context, String hint) {
SharedPreferences prefs = getPrefs(context);
return prefs.getBoolean(hint, false);
}
public static void resetHints(Context context) {
SharedPreferences prefs = getPrefs(context);
prefs.edit().clear().apply();
}
}

View File

@ -0,0 +1,33 @@
package com.etesync.syncadapter.utils
import android.content.Context
import android.content.SharedPreferences
import java.util.LinkedList
object HintManager {
private val PREF_NAME = "hints"
private fun getPrefs(context: Context): SharedPreferences {
return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
}
fun setHintSeen(context: Context, hint: String, seen: Boolean) {
val prefs = getPrefs(context)
prefs.edit().putBoolean(hint, seen).apply()
}
fun getHintSeen(context: Context, hint: String): Boolean {
val prefs = getPrefs(context)
return prefs.getBoolean(hint, false)
}
fun resetHints(context: Context) {
val prefs = getPrefs(context)
prefs.edit().clear().apply()
}
}

View File

@ -1,92 +0,0 @@
package com.etesync.syncadapter.utils;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import com.etesync.syncadapter.App;
import com.etesync.syncadapter.R;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Locale;
/**
* Created by tal on 11/09/17.
*/
public class LanguageUtils {
public static LocaleList getAppLanguages(Context context) {
Locale[] locales = Locale.getAvailableLocales();
Arrays.sort(locales, new Comparator<Locale>() {
@Override
public int compare(Locale aLocale, Locale aT1) {
return aLocale.getDisplayName().compareTo(aT1.getDisplayName());
}
});
String[] localeData = new String[locales.length + 1];
String[] displayNames = new String[locales.length + 1];
localeData[0] = App.DEFAULT_LANGUAGE;
displayNames[0] = context.getString(R.string.app_settings_force_language_default);
int index = 1;
for (Locale locale : locales) {
localeData[index] = encodeLocale(locale);
displayNames[index] = locale.getDisplayName();
index++;
}
return new LocaleList(localeData, displayNames);
}
public static void setLanguage(Context context, String locale) {
if (locale.equals(App.DEFAULT_LANGUAGE)) setLanguage(context, App.sDefaultLocacle);
else setLanguage(context, decodeLocale(locale));
}
@SuppressWarnings("deprecation")
public static void setLanguage(Context context, Locale locale) {
context = context.getApplicationContext();
Locale.setDefault(locale);
Configuration config = new Configuration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
config.setLocale(locale);
context.createConfigurationContext(config);
} else {
config.locale = locale;
context.getResources().updateConfiguration(
config, context.getResources().getDisplayMetrics());
}
context.getApplicationContext().getResources().updateConfiguration(config,
context.getResources().getDisplayMetrics());
}
private static String encodeLocale(Locale locale) {
return String.format("%s;%s;%s",
locale.getLanguage(), locale.getCountry(), locale.getVariant());
}
private static Locale decodeLocale(String encodedLocale) {
String[] data = encodedLocale.split(";", -1);
return new Locale(data[0], data[1], data[2]);
}
public static class LocaleList {
private final String[] mLocaleData;
private final String[] mDisplayNames;
LocaleList(String[] localeData, String[] displayName) {
mLocaleData = localeData;
mDisplayNames = displayName;
}
public String[] getDisplayNames() {
return mDisplayNames;
}
public String[] getLocaleData() {
return mLocaleData;
}
}
}

View File

@ -0,0 +1,72 @@
package com.etesync.syncadapter.utils
import android.content.Context
import android.content.res.Configuration
import android.os.Build
import com.etesync.syncadapter.App
import com.etesync.syncadapter.R
import java.util.Arrays
import java.util.Comparator
import java.util.Locale
/**
* Created by tal on 11/09/17.
*/
object LanguageUtils {
fun getAppLanguages(context: Context): LocaleList {
val locales = Locale.getAvailableLocales()
Arrays.sort(locales) { aLocale, aT1 -> aLocale.displayName.compareTo(aT1.displayName) }
val localeData = arrayOfNulls<String>(locales.size + 1)
val displayNames = arrayOfNulls<String>(locales.size + 1)
localeData[0] = App.DEFAULT_LANGUAGE
displayNames[0] = context.getString(R.string.app_settings_force_language_default)
var index = 1
for (locale in locales) {
localeData[index] = encodeLocale(locale)
displayNames[index] = locale.displayName
index++
}
return LocaleList(localeData as Array<String>, displayNames as Array<String>)
}
fun setLanguage(context: Context, locale: String) {
if (locale == App.DEFAULT_LANGUAGE)
setLanguage(context, App.sDefaultLocacle)
else
setLanguage(context, decodeLocale(locale))
}
fun setLanguage(context: Context, locale: Locale) {
var context = context
context = context.applicationContext
Locale.setDefault(locale)
val config = Configuration()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
config.setLocale(locale)
context.createConfigurationContext(config)
} else {
config.locale = locale
context.resources.updateConfiguration(
config, context.resources.displayMetrics)
}
context.applicationContext.resources.updateConfiguration(config,
context.resources.displayMetrics)
}
private fun encodeLocale(locale: Locale): String {
return String.format("%s;%s;%s",
locale.language, locale.country, locale.variant)
}
private fun decodeLocale(encodedLocale: String): Locale {
val data = encodedLocale.split(";".toRegex()).toTypedArray()
return Locale(data[0], data[1], data[2])
}
class LocaleList internal constructor(val localeData: Array<String>, val displayNames: Array<String>)
}

View File

@ -1,23 +0,0 @@
package com.etesync.syncadapter.utils;
import android.app.Activity;
import android.view.View;
import tourguide.tourguide.Overlay;
import tourguide.tourguide.Pointer;
import tourguide.tourguide.TourGuide;
public class ShowcaseBuilder {
public static TourGuide getBuilder(Activity activity) {
final TourGuide ret = TourGuide.init(activity).with(TourGuide.Technique.Click)
.setPointer(new Pointer());
ret.setOverlay(new Overlay().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ret.cleanUp();
}
}));
return ret;
}
}

View File

@ -0,0 +1,18 @@
package com.etesync.syncadapter.utils
import android.app.Activity
import android.view.View
import tourguide.tourguide.Overlay
import tourguide.tourguide.Pointer
import tourguide.tourguide.TourGuide
object ShowcaseBuilder {
fun getBuilder(activity: Activity): TourGuide {
val ret = TourGuide.init(activity).with(TourGuide.Technique.Click)
.setPointer(Pointer())
ret.setOverlay(Overlay().setOnClickListener { ret.cleanUp() })
return ret
}
}