display the initial apps with a default launcher

This commit is contained in:
tibbi 2016-08-13 14:44:08 +02:00
parent 2d72bab77f
commit c62cfd1b90
4 changed files with 75 additions and 58 deletions

View File

@ -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) {
}
}

View File

@ -1,43 +1,25 @@
package com.simplemobiletools.applauncher.activities package com.simplemobiletools.applauncher.activities
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import com.simplemobiletools.applauncher.R 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.isFirstRun
import com.simplemobiletools.applauncher.extensions.preferences import com.simplemobiletools.applauncher.extensions.preferences
import com.simplemobiletools.applauncher.models.AppLauncher
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
class MainActivity : SimpleActivity() { class MainActivity : SimpleActivity() {
lateinit var dbHelper: DbHelper
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
fillGrid() dbHelper = DbHelper(applicationContext)
} launchers_holder.adapter = MyCursorAdapter(applicationContext, dbHelper.getLaunchers()) {
private fun fillGrid() {
val apps = ArrayList<AppLauncher>()
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)
}
} }
} }

View File

@ -1,27 +1,36 @@
package com.simplemobiletools.applauncher.adapters package com.simplemobiletools.applauncher.adapters
import android.content.Context
import android.database.Cursor
import android.support.v7.widget.RecyclerView import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import com.simplemobiletools.applauncher.R import com.simplemobiletools.applauncher.R
import com.simplemobiletools.applauncher.databases.DbHelper
import com.simplemobiletools.applauncher.models.AppLauncher import com.simplemobiletools.applauncher.models.AppLauncher
import kotlinx.android.synthetic.main.app_launcher_item.view.* import kotlinx.android.synthetic.main.app_launcher_item.view.*
import java.util.*
class LaunchersAdapter(val launchers: ArrayList<AppLauncher>, val itemClick: (AppLauncher) -> Unit) : class MyCursorAdapter(cxt: Context, dataCursor: Cursor, val itemClick: (AppLauncher) -> Unit) : RecyclerView.Adapter<MyCursorAdapter.ViewHolder>() {
RecyclerView.Adapter<LaunchersAdapter.ViewHolder>() { val cursor = dataCursor
override fun onBindViewHolder(holder: ViewHolder, position: Int) { val context = cxt
holder.bindView(launchers[position])
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder { override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent?.context).inflate(R.layout.app_launcher_item, parent, false) val view = LayoutInflater.from(parent?.context).inflate(R.layout.app_launcher_item, parent, false)
return ViewHolder(view, itemClick) 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 { override fun getItemCount(): Int {
return launchers.count() return cursor.count
} }
class ViewHolder(view: View, val itemClick: (AppLauncher) -> Unit) : RecyclerView.ViewHolder(view) { class ViewHolder(view: View, val itemClick: (AppLauncher) -> Unit) : RecyclerView.ViewHolder(view) {

View File

@ -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) {
}
}