kotlin migration

This commit is contained in:
Mariotaku Lee 2017-12-04 15:56:45 +08:00
parent 48d52ca7ae
commit 22eca17b26
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
4 changed files with 139 additions and 153 deletions

View File

@ -1,132 +0,0 @@
package org.mariotaku.twidere.provider;
import android.Manifest;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore.MediaColumns;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import kotlin.collections.ArraysKt;
/**
* Created by mariotaku on 16/4/4.
*/
public class ShareProvider extends ContentProvider {
public static final String[] COLUMNS = {MediaColumns.DATA, MediaColumns.DISPLAY_NAME,
MediaColumns.SIZE, MediaColumns.MIME_TYPE};
@Override
public boolean onCreate() {
return true;
}
@Nullable
@Override
public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
try {
final File file = getFile(uri);
if (projection == null) {
projection = COLUMNS;
}
MatrixCursor cursor = new MatrixCursor(projection, 1);
Object[] values = new Object[projection.length];
writeValue(projection, values, MediaColumns.DATA, file.getAbsolutePath());
cursor.addRow(values);
return cursor;
} catch (IOException e) {
return null;
}
}
@Nullable
@Override
public ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode) throws FileNotFoundException {
if (!mode.equals("r")) throw new IllegalArgumentException();
final File file = getFile(uri);
return ParcelFileDescriptor.open(file,
ParcelFileDescriptor.MODE_READ_ONLY);
}
private void writeValue(String[] columns, Object[] values, String column, Object value) {
int idx = ArraysKt.indexOf(columns, column);
if (idx >= 0) {
values[idx] = value;
}
}
private File getFile(@NonNull Uri uri) throws FileNotFoundException {
final String lastPathSegment = uri.getLastPathSegment();
if (lastPathSegment == null) throw new FileNotFoundException(uri.toString());
return new File(getFilesDir(getContext()), lastPathSegment);
}
@Nullable
@Override
public String getType(@NonNull Uri uri) {
return null;
}
@Nullable
@Override
public Uri insert(@NonNull Uri uri, ContentValues values) {
return null;
}
@Override
public int delete(@NonNull Uri uri, String selection, String[] selectionArgs) {
return 0;
}
@Override
public int update(@NonNull Uri uri, ContentValues values, String selection, String[] selectionArgs) {
return 0;
}
@Nullable
public static File getFilesDir(Context context) {
File cacheDir = context.getCacheDir();
if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
PackageManager.PERMISSION_GRANTED) {
final File externalCacheDir = context.getExternalCacheDir();
if (externalCacheDir != null && externalCacheDir.canWrite()) {
cacheDir = externalCacheDir;
}
}
if (cacheDir == null) return null;
return new File(cacheDir, "shared_files");
}
@Nullable
public static Uri getUriForFile(@NonNull Context context, @NonNull String authority, @NonNull File file) {
final File filesDir = getFilesDir(context);
if (filesDir == null) return null;
if (!filesDir.equals(file.getParentFile())) return null;
return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(authority).appendPath(file.getName()).build();
}
public static boolean clearTempFiles(Context context) {
final File externalCacheDir = context.getExternalCacheDir();
if (externalCacheDir == null) return false;
File[] files = externalCacheDir.listFiles();
for (File file : files) {
if (file.isFile()) {
//noinspection ResultOfMethodCallIgnored
file.delete();
}
}
return true;
}
}

View File

@ -17,15 +17,17 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.provider;
package org.mariotaku.twidere.provider
import android.content.SearchRecentSuggestionsProvider;
import android.content.SearchRecentSuggestionsProvider
public class RecentSearchProvider extends SearchRecentSuggestionsProvider {
public final static String AUTHORITY = "org.mariotaku.twidere.provider.SearchRecentSuggestions";
public final static int MODE = DATABASE_MODE_QUERIES;
class RecentSearchProvider : SearchRecentSuggestionsProvider() {
init {
setupSuggestions(AUTHORITY, MODE)
}
public RecentSearchProvider() {
setupSuggestions(AUTHORITY, MODE);
}
companion object {
const val AUTHORITY = "org.mariotaku.twidere.provider.SearchRecentSuggestions"
const val MODE = SearchRecentSuggestionsProvider.DATABASE_MODE_QUERIES
}
}

View File

@ -0,0 +1,119 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2017 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.provider
import android.Manifest
import android.content.ContentProvider
import android.content.ContentResolver
import android.content.ContentValues
import android.content.Context
import android.content.pm.PackageManager
import android.database.Cursor
import android.database.MatrixCursor
import android.net.Uri
import android.os.ParcelFileDescriptor
import android.provider.MediaStore.MediaColumns
import android.support.v4.content.ContextCompat
import java.io.File
import java.io.FileNotFoundException
import java.io.IOException
class ShareProvider : ContentProvider() {
override fun onCreate(): Boolean {
return true
}
override fun query(uri: Uri, projection: Array<String>?, selection: String?,
selectionArgs: Array<String>?, sortOrder: String?): Cursor? {
val file = try {
getFile(uri)
} catch (e: IOException) {
return null
}
val realProjection = projection ?: defaultColumns
val cursor = MatrixCursor(realProjection, 1)
val values = arrayOfNulls<Any>(realProjection.size)
writeValue(realProjection, values, MediaColumns.DATA, file.absolutePath)
cursor.addRow(values)
return cursor
}
@Throws(FileNotFoundException::class)
override fun openFile(uri: Uri, mode: String): ParcelFileDescriptor {
if (mode != "r") throw IllegalArgumentException()
val file = getFile(uri)
return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY)
}
override fun getType(uri: Uri): String? = null
override fun insert(uri: Uri, values: ContentValues?): Uri? = null
override fun delete(uri: Uri, selection: String?, selectionArgs: Array<String>?): Int = 0
override fun update(uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array<String>?): Int = 0
private fun writeValue(columns: Array<String>, values: Array<Any?>, column: String, value: Any) {
val idx = columns.indexOf(column)
if (idx >= 0) {
values[idx] = value
}
}
@Throws(FileNotFoundException::class)
private fun getFile(uri: Uri): File {
val lastPathSegment = uri.lastPathSegment ?: throw FileNotFoundException(uri.toString())
return File(getFilesDir(context), lastPathSegment)
}
companion object {
private val defaultColumns = arrayOf(MediaColumns.DATA, MediaColumns.DISPLAY_NAME, MediaColumns.SIZE, MediaColumns.MIME_TYPE)
fun getFilesDir(context: Context?): File? {
var cacheDir: File? = context!!.cacheDir
if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
val externalCacheDir = context.externalCacheDir
if (externalCacheDir != null && externalCacheDir.canWrite()) {
cacheDir = externalCacheDir
}
}
return if (cacheDir == null) null else File(cacheDir, "shared_files")
}
fun getUriForFile(context: Context, authority: String, file: File): Uri? {
val filesDir = getFilesDir(context) ?: return null
return if (filesDir != file.parentFile) null else Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(authority).appendPath(file.name).build()
}
fun clearTempFiles(context: Context): Boolean {
val externalCacheDir = context.externalCacheDir ?: return false
val files = externalCacheDir.listFiles()
for (file in files) {
if (file.isFile) {
file.delete()
}
}
return true
}
}
}

View File

@ -17,22 +17,19 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.receiver;
package org.mariotaku.twidere.receiver
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import org.mariotaku.twidere.activity.HiddenSettingsActivity
import org.mariotaku.twidere.activity.HiddenSettingsActivity;
import org.mariotaku.twidere.constant.IntentConstants;
class SecretCodeBroadcastReceiver : BroadcastReceiver() {
public class SecretCodeBroadcastReceiver extends BroadcastReceiver implements IntentConstants {
@Override
public void onReceive(final Context context, final Intent intent) {
final Intent testIntent = new Intent(context, HiddenSettingsActivity.class);
testIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(testIntent);
override fun onReceive(context: Context, intent: Intent) {
val testIntent = Intent(context, HiddenSettingsActivity::class.java)
testIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(testIntent)
}
}