diff --git a/app/src/main/kotlin/com/simplemobiletools/applauncher/DbHelper.kt b/app/src/main/kotlin/com/simplemobiletools/applauncher/DbHelper.kt deleted file mode 100644 index 5809833..0000000 --- a/app/src/main/kotlin/com/simplemobiletools/applauncher/DbHelper.kt +++ /dev/null @@ -1,28 +0,0 @@ -package com.simplemobiletools.applauncher - -import android.content.Context -import android.database.sqlite.SQLiteDatabase -import android.database.sqlite.SQLiteOpenHelper - -class DbHelper(context: Context) : SQLiteOpenHelper(context, "launchers.db", null, 1) { - val TABLE = "launchers" - val CREATE_DB = "CREATE TABLE $TABLE (" + - "$ID integer PRIMARY KEY autoincrement," + - "$NAME TEXT," + - "$PKG_NAME TEXT" + - ")" - - companion object { - val ID: String = "_id" - val NAME: String = "name" - val PKG_NAME: String = "pkgName" - } - - override fun onCreate(db: SQLiteDatabase) { - db.execSQL(CREATE_DB) - } - - override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) { - - } -} diff --git a/app/src/main/kotlin/com/simplemobiletools/applauncher/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/applauncher/activities/MainActivity.kt index f8be2d5..9e0e851 100644 --- a/app/src/main/kotlin/com/simplemobiletools/applauncher/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/applauncher/activities/MainActivity.kt @@ -1,43 +1,25 @@ package com.simplemobiletools.applauncher.activities import android.content.Intent -import android.content.pm.PackageManager import android.os.Bundle import android.view.Menu import android.view.MenuItem import com.simplemobiletools.applauncher.R -import com.simplemobiletools.applauncher.adapters.LaunchersAdapter +import com.simplemobiletools.applauncher.adapters.MyCursorAdapter +import com.simplemobiletools.applauncher.databases.DbHelper import com.simplemobiletools.applauncher.extensions.isFirstRun import com.simplemobiletools.applauncher.extensions.preferences -import com.simplemobiletools.applauncher.models.AppLauncher import kotlinx.android.synthetic.main.activity_main.* -import java.util.* class MainActivity : SimpleActivity() { + lateinit var dbHelper: DbHelper override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) - fillGrid() - } + dbHelper = DbHelper(applicationContext) + launchers_holder.adapter = MyCursorAdapter(applicationContext, dbHelper.getLaunchers()) { - private fun fillGrid() { - val apps = ArrayList() - val pm = this.packageManager - val intent = Intent(Intent.ACTION_MAIN, null) - intent.addCategory(Intent.CATEGORY_LAUNCHER) - val list = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED) - for (info in list) { - val componentInfo = info.activityInfo.applicationInfo - apps.add(AppLauncher(componentInfo.loadLabel(pm).toString(), componentInfo.loadIcon(pm), componentInfo.packageName)) - } - - apps.sortBy { it.name } - launchers_holder.adapter = LaunchersAdapter(apps) { - val launchIntent = packageManager.getLaunchIntentForPackage(it.pkgName) - if (launchIntent != null) { - startActivity(launchIntent) - } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/applauncher/adapters/LaunchersAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/applauncher/adapters/MyCursorAdapter.kt similarity index 61% rename from app/src/main/kotlin/com/simplemobiletools/applauncher/adapters/LaunchersAdapter.kt rename to app/src/main/kotlin/com/simplemobiletools/applauncher/adapters/MyCursorAdapter.kt index e853ec2..342d0c0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/applauncher/adapters/LaunchersAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/applauncher/adapters/MyCursorAdapter.kt @@ -1,27 +1,36 @@ package com.simplemobiletools.applauncher.adapters +import android.content.Context +import android.database.Cursor import android.support.v7.widget.RecyclerView import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import com.simplemobiletools.applauncher.R +import com.simplemobiletools.applauncher.databases.DbHelper import com.simplemobiletools.applauncher.models.AppLauncher import kotlinx.android.synthetic.main.app_launcher_item.view.* -import java.util.* -class LaunchersAdapter(val launchers: ArrayList, val itemClick: (AppLauncher) -> Unit) : - RecyclerView.Adapter() { - override fun onBindViewHolder(holder: ViewHolder, position: Int) { - holder.bindView(launchers[position]) - } +class MyCursorAdapter(cxt: Context, dataCursor: Cursor, val itemClick: (AppLauncher) -> Unit) : RecyclerView.Adapter() { + val cursor = dataCursor + val context = cxt override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder { val view = LayoutInflater.from(parent?.context).inflate(R.layout.app_launcher_item, parent, false) return ViewHolder(view, itemClick) } + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + cursor.moveToPosition(position) + val name = cursor.getString(cursor.getColumnIndex(DbHelper.NAME)) + val pkgName = cursor.getString(cursor.getColumnIndex(DbHelper.PKG_NAME)) + val icon = context.resources.getDrawable(R.mipmap.launcher) + val launcher = AppLauncher(name, icon, pkgName) + holder.bindView(launcher) + } + override fun getItemCount(): Int { - return launchers.count() + return cursor.count } class ViewHolder(view: View, val itemClick: (AppLauncher) -> Unit) : RecyclerView.ViewHolder(view) { diff --git a/app/src/main/kotlin/com/simplemobiletools/applauncher/databases/DbHelper.kt b/app/src/main/kotlin/com/simplemobiletools/applauncher/databases/DbHelper.kt new file mode 100644 index 0000000..d56a247 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/applauncher/databases/DbHelper.kt @@ -0,0 +1,54 @@ +package com.simplemobiletools.applauncher.databases + +import android.content.ContentValues +import android.content.Context +import android.database.Cursor +import android.database.sqlite.SQLiteDatabase +import android.database.sqlite.SQLiteOpenHelper + +class DbHelper(context: Context) : SQLiteOpenHelper(context, "launchers.db", null, 1) { + val TABLE = "launchers" + val CREATE_DB = "CREATE TABLE $TABLE (" + + "$ID integer PRIMARY KEY autoincrement," + + "$NAME TEXT," + + "$PKG_NAME TEXT UNIQUE" + + ")" + + companion object { + val ID: String = "_id" + val NAME: String = "name" + val PKG_NAME: String = "pkgName" + } + + fun addInitialLaunchers(db:SQLiteDatabase) { + addLauncher("Simple Calculator", "com.simplemobiletools.calculator", db) + addLauncher("Simple Calendar", "com.simplemobiletools.calendar", db) + addLauncher("Simple Camera", "com.simplemobiletools.camera", db) + addLauncher("Simple Draw", "com.simplemobiletools.draw", db) + addLauncher("Simple File Manager", "com.simplemobiletools.filemanager", db) + addLauncher("Simple Flashlight", "com.simplemobiletools.flashlight", db) + addLauncher("Simple Gallery", "com.simplemobiletools.gallery", db) + addLauncher("Simple Music Player", "com.simplemobiletools.musicplayer", db) + addLauncher("Simple Notes", "com.simplemobiletools.notes", db) + } + + fun addLauncher(name: String, pkgName: String, db: SQLiteDatabase = writableDatabase) { + val contentValues = ContentValues() + contentValues.put(NAME, name) + contentValues.put(PKG_NAME, pkgName) + db.insert(TABLE, null, contentValues) + } + + fun getLaunchers(): Cursor { + return readableDatabase.query(TABLE, arrayOf(NAME, PKG_NAME), null, null, null, null, NAME) + } + + override fun onCreate(db: SQLiteDatabase) { + db.execSQL(CREATE_DB) + addInitialLaunchers(db) + } + + override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) { + + } +}