Skip to content

Commit

Permalink
Removed asynctask
Browse files Browse the repository at this point in the history
Removed anko async task and replaced with coroutine.
  • Loading branch information
BharathVishal committed Apr 24, 2020
1 parent 51524bf commit fd38999
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 70 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
9 changes: 5 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
vectorDrawables.useSupportLibrary = true
minSdkVersion 21
targetSdkVersion 29
versionCode 5
versionName "1.0"
versionCode 9
versionName "1.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand All @@ -32,9 +32,10 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation "org.jetbrains.anko:anko-commons:0.10.4"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.5'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.5"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation 'com.google.android.material:material:1.2.0-alpha06'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.13'
implementation 'com.github.bumptech.glide:glide:4.11.0'
Expand Down
142 changes: 76 additions & 66 deletions app/src/main/java/com/bharathvishal/appmanager/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import com.bharathvishal.appmanager.Classes.AppManager
import com.bharathvishal.appmanager.Constants.Constants
import com.bharathvishal.appmanager.R.array.spinner_app_type
import kotlinx.android.synthetic.main.activity_main.*
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
import kotlinx.coroutines.*
import java.lang.ref.WeakReference
import java.util.*


class MainActivity : AppCompatActivity() {
class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {
private var adapter: AppsAdapter? = null

internal lateinit var appList: MutableList<AppInfo>
Expand All @@ -35,7 +35,7 @@ class MainActivity : AppCompatActivity() {
private var apkInfoExtractor: ApkInfoExtractor? = null

internal var arrAppType: Array<String>? = null
private var recyclerViewLayoutManager: androidx.recyclerview.widget.RecyclerView.LayoutManager? = null
private var recyclerViewLayoutManager: RecyclerView.LayoutManager? = null
internal var numberOfUserApps: String? = Constants.STRING_EMPTY
internal var numberOfSystemApps: String? = Constants.STRING_EMPTY

Expand All @@ -58,93 +58,103 @@ class MainActivity : AppCompatActivity() {
spinner_App_Type.adapter = spinnerArrayAdapter

apkInfoExtractor = ApkInfoExtractor(this)
recyclerViewLayoutManager = androidx.recyclerview.widget.GridLayoutManager(actvityContext, 1)
recyclerViewLayoutManager = GridLayoutManager(actvityContext, 1)


getApps(actvityContext as MainActivity)
getApps(actvityContext)

spinner_App_Type.isSelected = false
spinner_App_Type.isEnabled = false

}


private fun getApps(context: Context) {
fun getApps(context: Context) {
val contextRef: WeakReference<Context> = WeakReference(context)

doAsync {
appManOb = ApkInfoExtractor(context).appManagerInitValues()
//Coroutine
launch(Dispatchers.Default) {
try {
val context1 = contextRef.get()

if (appManOb != null) {
numberOfUserApps = Constants.STRING_EMPTY + appManOb!!.userAppSize
numberOfSystemApps = Constants.STRING_EMPTY + appManOb!!.systemAppSize
appManOb = ApkInfoExtractor(context).appManagerInitValues()

userAppList.addAll(appManOb!!.userApps)
systemAppList.addAll(appManOb!!.systemApps)
if (appManOb != null) {
numberOfUserApps = Constants.STRING_EMPTY + appManOb!!.userAppSize
numberOfSystemApps = Constants.STRING_EMPTY + appManOb!!.systemAppSize

appListAlternate.addAll(userAppList)
appList.addAll(userAppList)
userAppList.addAll(appManOb!!.userApps)
systemAppList.addAll(appManOb!!.systemApps)

adapter = AppsAdapter(context, appListAlternate)
} else {
appListAlternate.addAll(userAppList)
appList.addAll(userAppList)

numberOfUserApps = Constants.STRING_EMPTY + "0"
numberOfSystemApps = Constants.STRING_EMPTY + "0"
adapter = AppsAdapter(context, appListAlternate)
} else {

userAppList.clear()
systemAppList.clear()
appListAlternate.clear()
appList.clear()
numberOfUserApps = Constants.STRING_EMPTY + "0"
numberOfSystemApps = Constants.STRING_EMPTY + "0"

adapter = AppsAdapter(context, appListAlternate)
}
userAppList.clear()
systemAppList.clear()
appListAlternate.clear()
appList.clear()

uiThread {
recycler_view_Apps.layoutManager = recyclerViewLayoutManager

if (adapter!!.itemCount > 0) {
recycler_view_Apps.adapter = adapter
val text = "$numberOfUserApps User apps"
app_Counter_App_Manager.text = text

spinner_App_Type.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {

val selectedItem = parent.getItemAtPosition(position).toString()

if (selectedItem == arrAppType!![0]) {
//User Apps
val textUser = "$numberOfUserApps User apps"
app_Counter_App_Manager.text = textUser
appList.clear()
appList.addAll(userAppList)
adapter?.updateList(userAppList)
} else if (selectedItem == arrAppType!![1]) {
//System Apps
val textSystem = "$numberOfSystemApps System apps"
app_Counter_App_Manager.text = textSystem
appList.clear()
appList.addAll(systemAppList)
adapter?.updateList(systemAppList)
}
} // to close the onItemSelected
adapter = AppsAdapter(context, appListAlternate)
}

//UI Thread
withContext(Dispatchers.Main) {

recycler_view_Apps.layoutManager = recyclerViewLayoutManager

if (adapter!!.itemCount > 0) {
recycler_view_Apps.adapter = adapter
val text = "$numberOfUserApps User apps"
app_Counter_App_Manager.text = text

spinner_App_Type.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {

override fun onNothingSelected(parent: AdapterView<*>) {
val selectedItem = parent.getItemAtPosition(position).toString()

if (selectedItem == arrAppType!![0]) {
//User Apps
val textUser = "$numberOfUserApps User apps"
app_Counter_App_Manager.text = textUser
appList.clear()
appList.addAll(userAppList)
adapter?.updateList(userAppList)
} else if (selectedItem == arrAppType!![1]) {
//System Apps
val textSystem = "$numberOfSystemApps System apps"
app_Counter_App_Manager.text = textSystem
appList.clear()
appList.addAll(systemAppList)
adapter?.updateList(systemAppList)
}
} // to close the onItemSelected

override fun onNothingSelected(parent: AdapterView<*>) {

}
}
}

spinner_App_Type.isEnabled = true
spinner_App_Type.setSelection(0, true)
spinner_App_Type.isEnabled = true
spinner_App_Type.setSelection(0, true)

} else {
app_Counter_App_Manager.text = getString(R.string.No_Apps)
apps_recycler_layoout_ll.visibility = View.GONE
recycler_view_Apps.visibility = View.GONE
spinner_App_Type.isEnabled = false
list_empty_Apps_Appmanager.visibility = View.VISIBLE
}

} else {
app_Counter_App_Manager.text = getString(R.string.No_Apps)
apps_recycler_layoout_ll.visibility = View.GONE
recycler_view_Apps.visibility = View.GONE
spinner_App_Type.isEnabled = false
list_empty_Apps_Appmanager.visibility = View.VISIBLE
}
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
}
}

}

0 comments on commit fd38999

Please sign in to comment.