com.landawn.abacus.android.util.Fu Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-android Show documentation
Show all versions of abacus-android Show documentation
A general and simple library for Android
/*
* Copyright (C) 2016 HaiYang Li
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.landawn.abacus.android.util;
import java.io.Closeable;
import java.io.File;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import com.landawn.abacus.DataSet;
import com.landawn.abacus.android.util.Async.UIExecutor;
import com.landawn.abacus.android.util.SQLiteExecutor.Type;
import com.landawn.abacus.logging.Logger;
import com.landawn.abacus.logging.LoggerFactory;
import com.landawn.abacus.util.IOUtil;
import com.landawn.abacus.util.N;
import com.landawn.abacus.util.NamingPolicy;
import com.landawn.abacus.util.Try;
import com.landawn.abacus.util.function.Consumer;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.Dialog;
import android.app.KeyguardManager;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.res.Configuration;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.CancellationSignal;
import android.os.Looper;
import android.os.StatFs;
import android.util.DisplayMetrics;
import android.view.Surface;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
/**
* Always remember to initialize {@code Fu} class by calling method {@code init(Context context)} when the application is started.
*
* More:
*
* @since 0.8
*
* @author Haiyang Li
*
* @see awesome-android-ui
* @see awesome-android-libraries
* @see AndroidUtilCode
* @see android-utils
* @see xUtils3
*/
public class Fu {
static final Logger logger = LoggerFactory.getLogger(Fu.class);
private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
protected static volatile Context context;
private static volatile int MAX_APP_MEMORY;
public static final Runnable EMPTY_ACTION = new Runnable() {
@Override
public void run() {
// Do nothing;
}
};
public static final Consumer ON_ERROR_MISSING = new Consumer() {
@Override
public void accept(Exception t) {
throw new RuntimeException(t);
}
};
private Fu() {
// singleton.
}
public static synchronized void init(Context context) {
if (Fu.context != null) {
return;
}
Fu.context = context;
final ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
boolean isLargeHeap = (context.getApplicationInfo().flags & ApplicationInfo.FLAG_LARGE_HEAP) != 0;
int memoryClass = am.getMemoryClass();
if (isLargeHeap && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
memoryClass = am.getLargeMemoryClass();
}
MAX_APP_MEMORY = memoryClass;
}
public static Context context() {
return context;
}
public static int cupCount() {
return CPU_COUNT;
}
/**
* Copied from Picasso: http://square.github.io/picasso Copyright (C) 2013 Square, Inc.
*
* @param context
* @return approximate per-application memory in megabytes.
*/
public static int maxMemoryPerApp() {
return MAX_APP_MEMORY;
}
/**
* @param dir
* @return available size in megabytes.
*/
public static int freeDiskSpace() {
final StatFs stat = new StatFs(context.getFilesDir().getPath());
final long bytesAvailable = stat.getBlockSizeLong() * stat.getAvailableBlocksLong();
return (int) (bytesAvailable / (1024 * 1024));
}
/**
* @param dir
* @return available size in megabytes.
*/
public static int freeDiskSpace(File dir) {
final StatFs stat = new StatFs(dir.getPath());
final long bytesAvailable = stat.getBlockSizeLong() * stat.getAvailableBlocksLong();
return (int) (bytesAvailable / (1024 * 1024));
}
/**
*
* @return
* @see android.os.Build.BRAND
*/
public static String deviceBrand() {
return Build.BRAND;
}
/**
*
* @return
* @see android.os.Build.MODEL
*/
public static String deviceModel() {
return Build.MODEL;
}
/**
*
* @return
* @see android.os.Build.MANUFACTURER
*/
public static String deviceManufacturer() {
return Build.MANUFACTURER;
}
/**
*
* @return *
* @see android.os.Build.SERIAL
*/
public static String deviceSerialNumber() {
return Build.SERIAL;
}
// /**
// *
// * @return
// * @see android.os.Build.VERSION.RELEASE
// */
// public static String releaseVersion() {
// return Build.VERSION.RELEASE;
// }
//
// /**
// *
// * @return
// * @see android.os.Build.VERSION.BASE_OS
// */
// public static String baseOS() {
// return Build.VERSION.BASE_OS;
// }
/**
*
* @param targetClass
* @param cursor
* @return
*
* @see SQLiteExecutor#extractData(Class, Cursor)
*/
public static DataSet extractData(final Class> targetClass, final Cursor cursor) {
return SQLiteExecutor.extractData(targetClass, cursor);
}
/**
*
* @param targetClass an entity class with getter/setter methods.
* @param cursor
* @param offset
* @param count
* @return
*
* @see SQLiteExecutor#extractData(Class, Cursor, int, int)
*/
public static DataSet extractData(final Class> targetClass, final Cursor cursor, final int offset, final int count) {
return SQLiteExecutor.extractData(targetClass, cursor, offset, count);
}
/**
*
* @param cursor
* @param selectColumnTypes
* @return
*
*
* @see SQLiteExecutor#extractData(Cursor, Class[])
*/
@SuppressWarnings("rawtypes")
public static DataSet extractData(final Cursor cursor, final Class[] selectColumnTypes) {
return SQLiteExecutor.extractData(cursor, selectColumnTypes);
}
/**
*
* @param cursor
* @param selectColumnTypes
* @param offset
* @param count
* @return
*
* @see SQLiteExecutor#extractData(Cursor, Class[], int, int)
*/
@SuppressWarnings("rawtypes")
public static DataSet extractData(final Cursor cursor, final Class[] selectColumnTypes, final int offset, final int count) {
return SQLiteExecutor.extractData(cursor, selectColumnTypes, offset, count);
}
/**
*
* @param cursor
* @param selectColumnTypes
* @return
*
* @see SQLiteExecutor#extractData(Cursor, Collection)
*/
@SuppressWarnings("rawtypes")
public static DataSet extractData(final Cursor cursor, final Collection selectColumnTypes) {
return SQLiteExecutor.extractData(cursor, selectColumnTypes);
}
/**
*
* @param cursor
* @param selectColumnTypes
* @param offset
* @param count
* @return
*
* @see SQLiteExecutor#extractData(Cursor, Collection, int, int)
*/
@SuppressWarnings("rawtypes")
public static DataSet extractData(final Cursor cursor, final Collection selectColumnTypes, final int offset, final int count) {
return SQLiteExecutor.extractData(cursor, selectColumnTypes, offset, count);
}
/**
* Returns values from all rows associated with the specified targetClass
if the specified targetClass
is an entity class, otherwise, only returns values from first column.
*
* @param targetClass entity class or specific column type.
* @param cursor
* @return
*
* @see SQLiteExecutor#toList(Class, Cursor)
*/
public static List toList(final Class targetClass, final Cursor cursor) {
return SQLiteExecutor.toList(targetClass, cursor);
}
/**
* Returns values from all rows associated with the specified targetClass
if the specified targetClass
is an entity class, otherwise, only returns values from first column.
*
* @param targetClass entity class or specific column type.
* @param cursor
* @param offset
* @param count
* @return
*
* @see SQLiteExecutor#toList(Class, Cursor, int, int)
*/
public static List toList(final Class targetClass, final Cursor cursor, final int offset, final int count) {
return SQLiteExecutor.toList(targetClass, cursor, offset, count);
}
/**
* Returns the values from the specified column
.
*
* @param targetClass entity class or specific column type.
* @param cursor
* @param columnIndex
* @return
*
* @see SQLiteExecutor#toList(Class, Cursor, int)
*/
public static List toList(Class targetClass, Cursor cursor, int columnIndex) {
return SQLiteExecutor.toList(targetClass, cursor, columnIndex);
}
/**
* Returns the values from the specified column
.
*
* @param targetClass entity class or specific column type.
* @param cursor
* @param columnIndex
* @param offset
* @param count
* @return
*
* @see SQLiteExecutor#toList(Class, Cursor, int, int, int)
*/
public static List toList(final Class targetClass, final Cursor cursor, final int columnIndex, final int offset, final int count) {
return SQLiteExecutor.toList(targetClass, cursor, columnIndex, offset, count);
}
/**
*
* @param targetClass entity class with getter/setter methods.
* @param cursor
* @return
*
* @see SQLiteExecutor#toEntity(Class, Cursor)
*/
public static T toEntity(Class targetClass, Cursor cursor) {
return SQLiteExecutor.toEntity(targetClass, cursor);
}
/**
*
* @param targetClass entity class with getter/setter methods.
* @param cursor
* @param rowNum
* @return
*
* @see SQLiteExecutor#toEntity(Class, Cursor, int)
*/
public static T toEntity(Class targetClass, Cursor cursor, int rowNum) {
return SQLiteExecutor.toEntity(targetClass, cursor, rowNum);
}
/**
*
* @param targetClass
* @param contentValues
* @return
*
* @see SQLiteExecutor#toEntity(Class, ContentValues)
*/
public static T toEntity(final Class targetClass, final ContentValues contentValues) {
return SQLiteExecutor.toEntity(targetClass, contentValues);
}
/**
*
* @param targetClass an Map class or Entity class with getter/setter methods.
* @param contentValues
* @param namingPolicy
* @return
*
* @see SQLiteExecutor#toEntity(Class, ContentValues, NamingPolicy)
*/
public static T toEntity(final Class targetClass, final ContentValues contentValues, final NamingPolicy namingPolicy) {
return SQLiteExecutor.toEntity(targetClass, contentValues, namingPolicy);
}
/**
*
* @param obj
* @return
*
* @see SQLiteExecutor#toContentValues(Object)
*/
public static ContentValues toContentValues(final Object obj) {
return SQLiteExecutor.toContentValues(obj, null);
}
/**
*
* @param obj
* @param ignoredPropNames
* @return
*
* @see SQLiteExecutor#toContentValues(Object)
*/
public static ContentValues toContentValues(final Object obj, final Collection ignoredPropNames) {
return SQLiteExecutor.toContentValues(obj, ignoredPropNames);
}
/**
*
* @param obj an instance of Map or Entity.
* @param namingPolicy
* @return
*
* @see SQLiteExecutor#toContentValues(Object, NamingPolicy)
*/
public static ContentValues toContentValues(final Object obj, final NamingPolicy namingPolicy) {
return SQLiteExecutor.toContentValues(obj, null, namingPolicy);
}
/**
*
* @param obj an instance of Map or Entity.
* @param ignoredPropNames
* @param namingPolicy
* @return
*
* @see SQLiteExecutor#toContentValues(Object, NamingPolicy)
*/
public static ContentValues toContentValues(final Object obj, final Collection ignoredPropNames, final NamingPolicy namingPolicy) {
return SQLiteExecutor.toContentValues(obj, ignoredPropNames, namingPolicy);
}
public static final ContentValues asContentValues(String key1, Object value1) {
final ContentValues result = new ContentValues();
if (value1 == null) {
result.putNull(key1);
} else {
Type.valueOf(value1.getClass()).set(result, key1, value1);
}
return result;
}
public static final ContentValues asContentValues(String key1, Object value1, String key2, Object value2) {
final ContentValues result = new ContentValues();
if (value1 == null) {
result.putNull(key1);
} else {
Type.valueOf(value1.getClass()).set(result, key1, value1);
}
if (value2 == null) {
result.putNull(key2);
} else {
Type.valueOf(value2.getClass()).set(result, key2, value2);
}
return result;
}
public static final ContentValues asContentValues(String key1, Object value1, String key2, Object value2, String key3, Object value3) {
final ContentValues result = new ContentValues();
if (value1 == null) {
result.putNull(key1);
} else {
Type.valueOf(value1.getClass()).set(result, key1, value1);
}
if (value2 == null) {
result.putNull(key2);
} else {
Type.valueOf(value2.getClass()).set(result, key2, value2);
}
if (value3 == null) {
result.putNull(key3);
} else {
Type.valueOf(value3.getClass()).set(result, key3, value3);
}
return result;
}
@SafeVarargs
public static final ContentValues asContentValues(final Object... a) {
if (N.isNullOrEmpty(a)) {
return new ContentValues();
}
final ContentValues result = new ContentValues();
if ((a.length % 2) != 0) {
throw new IllegalArgumentException(
"The parameters must be the pairs of property name and value, or Map, or an entity class with getter/setter methods.");
}
@SuppressWarnings("rawtypes")
Type type = null;
String key = null;
Object value = null;
for (int i = 0, len = a.length; i < len; i++) {
key = (String) a[i];
value = a[++i];
if (a[i] == null) {
result.putNull(key);
} else {
type = Type.valueOf(value.getClass());
type.set(result, key, value);
}
}
return result;
}
@SuppressWarnings("rawtypes")
public static Map asColumnTypes(String c1, Class t1) {
return N.asMap(c1, t1);
}
@SuppressWarnings("rawtypes")
public static Map asColumnTypes(String c1, Class t1, String c2, Class t2) {
return N.asMap(c1, t1, c2, t2);
}
@SuppressWarnings("rawtypes")
public static Map asColumnTypes(String c1, Class t1, String c2, Class t2, String c3, Class t3) {
return N.asMap(c1, t1, c2, t2, c3, t3);
}
@SuppressWarnings("rawtypes")
@SafeVarargs
public static Map asColumnTypes(final Object... a) {
return N.asMap(a);
}
public static ContentResolver getContentResolver() {
return context.getContentResolver();
}
/**
* Query by context.getContentResolver().
*
* @param targetClass
* @param uri
* @param projection
* @return
*/
public static List query(Class targetClass, Uri uri, String projection) {
return query(targetClass, uri, projection, null, null, null);
}
/**
* Query by context.getContentResolver().
*
* @param targetClass
* @param uri
* @param projection
* @param selection
* @param selectionArgs
* @param sortOrder
* @return
*/
public static List query(Class targetClass, Uri uri, String projection, String selection, String[] selectionArgs, String sortOrder) {
return query(targetClass, uri, projection, selection, selectionArgs, sortOrder, null);
}
/**
* Query by context.getContentResolver().
*
* @param targetClass entity class or specific column type.
* @param uri
* @param projection
* @param selection
* @param selectionArgs
* @param sortOrder
* @param cancellationSignal
* @return
*/
public static List query(Class targetClass, final Uri uri, String projection, String selection, String[] selectionArgs, String sortOrder,
CancellationSignal cancellationSignal) {
final Cursor cursor = getContentResolver().query(uri, N.asArray(projection), selection, selectionArgs, sortOrder, cancellationSignal);
try {
return toList(targetClass, cursor);
} finally {
closeQuietly(cursor);
}
}
/**
* Query by context.getContentResolver().
*
* @param targetClass entity class with getter/setting method.
* @param uri
* @param projection
* @return
*/
public static List query(Class targetClass, final Uri uri, String[] projection) {
return query(targetClass, uri, projection, null, null, null);
}
/**
* Query by context.getContentResolver().
*
* @param targetClass entity class with getter/setting method.
* @param uri
* @param projection
* @param selection
* @param selectionArgs
* @param sortOrder
* @return
*/
public static List query(Class targetClass, final Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
return query(targetClass, uri, projection, selection, selectionArgs, sortOrder, null);
}
/**
* Query by context.getContentResolver().
*
* @param targetClass entity class with getter/setting method.
* @param uri
* @param projection
* @param selection
* @param selectionArgs
* @param sortOrder
* @param cancellationSignal
* @return
*/
public static List query(Class targetClass, final Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder,
CancellationSignal cancellationSignal) {
final Cursor cursor = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
try {
return toList(targetClass, cursor);
} finally {
closeQuietly(cursor);
}
}
/**
* Query by context.getContentResolver().
*
* @param uri
* @param projectionTypeMap
* @param selection
* @param selectionArgs
* @param sortOrder
* @return
*/
@SuppressWarnings("rawtypes")
public static List