com.landawn.abacus.android.util.CommonUtils 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.ObserverX.AutoCompleteTextViewObserver;
import com.landawn.abacus.android.util.ObserverX.MenuItemObserver;
import com.landawn.abacus.android.util.ObserverX.SearchViewObserver;
import com.landawn.abacus.android.util.ObserverX.TextViewObserver;
import com.landawn.abacus.android.util.ObserverX.ViewGroupObserver;
import com.landawn.abacus.android.util.ObserverX.ViewObserver;
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.Throwables;
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.MenuItem;
import android.view.Surface;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;
// TODO: Auto-generated Javadoc
/**
* Always remember to initialize {@code Fu} class by calling method {@code init(Context context)} when the application is started.
*
* More:
*
* @author Haiyang Li
* @see awesome-android-ui
* @see awesome-android-libraries
* @see AndroidUtilCode
* @see android-utils
* @see xUtils3
* @since 0.8
*/
public class CommonUtils {
static final Logger logger = LoggerFactory.getLogger(CommonUtils.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 CommonUtils() {
// singleton.
}
/**
*
* @param context
*/
public static synchronized void init(Context context) {
if (CommonUtils.context != null) {
return;
}
CommonUtils.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.
*
* @return approximate per-application memory in megabytes.
*/
public static int maxMemoryPerApp() {
return MAX_APP_MEMORY;
}
/**
* Free disk space.
*
* @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));
}
/**
* Free disk space.
*
* @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;
}
/**
* Device serial number.
*
* @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
* @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
* @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
* @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
* @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
* @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
* @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
* @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
* @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);
}
/**
* To content values.
*
* @param obj
* @return
* @see SQLiteExecutor#toContentValues(Object)
*/
public static ContentValues toContentValues(final Object obj) {
return SQLiteExecutor.toContentValues(obj, null);
}
/**
* To content values.
*
* @param obj
* @param ignoredPropNames
* @return
* @see SQLiteExecutor#toContentValues(Object)
*/
public static ContentValues toContentValues(final Object obj, final Collection ignoredPropNames) {
return SQLiteExecutor.toContentValues(obj, ignoredPropNames);
}
/**
* To content values.
*
* @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);
}
/**
* To content values.
*
* @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);
}
/**
* As content values.
*
* @param key1
* @param value1
* @return
*/
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;
}
/**
* As content values.
*
* @param key1
* @param value1
* @param key2
* @param value2
* @return
*/
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;
}
/**
* As content values.
*
* @param key1
* @param value1
* @param key2
* @param value2
* @param key3
* @param value3
* @return
*/
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;
}
/**
* As content values.
*
* @param a
* @return
* @deprecated
*/
@Deprecated
@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;
}
/**
* As column types.
*
* @param c1
* @param t1
* @return
*/
@SuppressWarnings("rawtypes")
public static Map asColumnTypes(String c1, Class t1) {
return N.asMap(c1, t1);
}
/**
* As column types.
*
* @param c1
* @param t1
* @param c2
* @param t2
* @return
*/
@SuppressWarnings("rawtypes")
public static Map asColumnTypes(String c1, Class t1, String c2, Class t2) {
return N.asMap(c1, t1, c2, t2);
}
/**
* As column types.
*
* @param c1
* @param t1
* @param c2
* @param t2
* @param c3
* @param t3
* @return
*/
@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);
}
/**
* As column types.
*
* @param a
* @return
* @deprecated
*/
@Deprecated
@SuppressWarnings({ "rawtypes" })
@SafeVarargs
public static Map asColumnTypes(final Object... a) {
return N.asMap(a);
}
/**
* Gets the content resolver.
*
* @return
*/
public static ContentResolver getContentResolver() {
return context.getContentResolver();
}
/**
* Query by context.getContentResolver().
*
* @param
* @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
* @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
* @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
* @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
* @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
* @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
* @return
*/
@SuppressWarnings("rawtypes")
public static List