version:1.0.0
update:更换包名 bugfixes:
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
package com.xxpatx.os.iconpostition;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteOpenHelper;
|
||||
import android.os.Environment;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class IconPositionDBHelper extends SQLiteOpenHelper {
|
||||
public static final String DATABASE_NAME = "requestlog.db";
|
||||
private static final String DATABASE_FILE_NAME = Environment.getExternalStorageDirectory().getPath() + File.separator + DATABASE_NAME;
|
||||
private static final int DATABASE_VERSION = 1;
|
||||
|
||||
public IconPositionDBHelper(Context context) {
|
||||
// super(context, DATABASE_NAME, null, DATABASE_VERSION);
|
||||
super(context, DATABASE_FILE_NAME, null, DATABASE_VERSION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(SQLiteDatabase sqLiteDatabase) {
|
||||
sqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS DesktopIcon (" +
|
||||
"id integer primary key autoincrement," +
|
||||
"name text," +
|
||||
"package text not null," +
|
||||
"icon blob," +
|
||||
"position integer UNIQUE" +
|
||||
")"
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package com.xxpatx.os.iconpostition;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
|
||||
import com.xxpatx.os.bean.NetDesktopIcon;
|
||||
import com.xxpatx.os.utils.ApkUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class IconPositionManager {
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
private static IconPositionManager sInstance;
|
||||
private Context mContext;
|
||||
private IconPositionDBHelper mDBHelper;
|
||||
|
||||
private static final String TABLE_NAME = "DesktopIcon";
|
||||
private static String TAG = IconPositionManager.class.getSimpleName();
|
||||
|
||||
private IconPositionManager(Context context) {
|
||||
if (context == null) {
|
||||
throw new RuntimeException("Context is NULL");
|
||||
}
|
||||
mContext = context;
|
||||
mDBHelper = new IconPositionDBHelper(context);
|
||||
}
|
||||
|
||||
public static void init(Context context) {
|
||||
if (sInstance == null) {
|
||||
sInstance = new IconPositionManager(context);
|
||||
}
|
||||
}
|
||||
|
||||
public static IconPositionManager getInstance() {
|
||||
if (sInstance == null) {
|
||||
throw new IllegalStateException("You must be init IconPositionManager first");
|
||||
}
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
public IconPositionDBHelper getDBHelper() {
|
||||
if (mDBHelper == null) {
|
||||
mDBHelper = new IconPositionDBHelper(mContext);
|
||||
}
|
||||
return mDBHelper;
|
||||
}
|
||||
|
||||
public void setOnlinePosition(List<NetDesktopIcon> desktopIconList) {
|
||||
HashMap hashMap = new HashMap();
|
||||
|
||||
List<NetDesktopIcon> dbList = getLogList();
|
||||
for (int i = 0; i < dbList.size(); i++) {
|
||||
NetDesktopIcon icon = dbList.get(i);
|
||||
if (!ApkUtils.isAvailable(mContext, icon.getPackages())) {
|
||||
dbList.remove(i);
|
||||
deleteIcon(icon.getPackages());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<NetDesktopIcon> getLogList() {
|
||||
List<NetDesktopIcon> logBeanList = new ArrayList<>();
|
||||
SQLiteDatabase db = mDBHelper.getReadableDatabase();
|
||||
String sql = "select * from " + TABLE_NAME;
|
||||
Cursor cursor = null;
|
||||
try {
|
||||
cursor = db.rawQuery(sql, null);
|
||||
while (cursor.moveToNext()) {
|
||||
NetDesktopIcon desktopIcon = new NetDesktopIcon();
|
||||
desktopIcon.setId(cursor.getInt(cursor.getColumnIndex("id")));
|
||||
desktopIcon.setName(cursor.getString(cursor.getColumnIndex("name")));
|
||||
desktopIcon.setPackages(cursor.getString(cursor.getColumnIndex("package")));
|
||||
desktopIcon.setNum(cursor.getInt(cursor.getColumnIndex("position")));
|
||||
desktopIcon.setIcon(IconUtils.Bytes2Bimap(cursor.getBlob(cursor.getColumnIndex("icon"))));
|
||||
logBeanList.add(desktopIcon);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (cursor != null) {
|
||||
cursor.close();
|
||||
}
|
||||
db.close();
|
||||
}
|
||||
return logBeanList;
|
||||
}
|
||||
|
||||
public void updateIcon(NetDesktopIcon netDesktopIcon) {
|
||||
SQLiteDatabase db = mDBHelper.getWritableDatabase();
|
||||
ContentValues values = new ContentValues();
|
||||
values.put("name", netDesktopIcon.getName());
|
||||
values.put("package", netDesktopIcon.getPackages());
|
||||
values.put("position", netDesktopIcon.getNum());
|
||||
values.put("icon", IconUtils.getPackageIcon(mContext, netDesktopIcon.getPackages()));
|
||||
db.update(TABLE_NAME, values, "package=?", new String[]{netDesktopIcon.getPackages()});
|
||||
}
|
||||
|
||||
public void deleteIcon(String pkg) {
|
||||
SQLiteDatabase db = mDBHelper.getWritableDatabase();
|
||||
db.delete(TABLE_NAME, "package=?", new String[]{pkg});
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
42
app/src/main/java/com/xxpatx/os/iconpostition/IconUtils.java
Normal file
42
app/src/main/java/com/xxpatx/os/iconpostition/IconUtils.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package com.xxpatx.os.iconpostition;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
public class IconUtils {
|
||||
public static byte[] getPackageIcon(Context context, String pkg) {
|
||||
PackageManager pm = context.getPackageManager();
|
||||
ApplicationInfo applicationInfo = null;
|
||||
try {
|
||||
applicationInfo = pm.getApplicationInfo(pkg, PackageManager.GET_META_DATA);
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (applicationInfo != null) {
|
||||
Drawable icon = applicationInfo.loadIcon(pm);
|
||||
Bitmap bitmap = ((BitmapDrawable) icon).getBitmap();
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
|
||||
byte[] bitmapdata = stream.toByteArray();
|
||||
return bitmapdata;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static Bitmap Bytes2Bimap(byte[] b) {
|
||||
if (b.length != 0) {
|
||||
return BitmapFactory.decodeByteArray(b, 0, b.length);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user