Commit 2a3482a9 authored by Nihar Thakkar's avatar Nihar Thakkar
Browse files

Request storage permission from user

parent 3628e2c0
......@@ -5,6 +5,7 @@ import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.content.pm.PackageManager
import android.os.Bundle
import android.os.IBinder
import android.os.Message
......@@ -15,6 +16,7 @@ import android.support.design.widget.BottomNavigationView
import android.support.v4.app.Fragment
import android.support.v7.app.AppCompatActivity
import android.view.MenuItem
import android.widget.Toast
import io.eelo.appinstaller.application.model.InstallManager
import io.eelo.appinstaller.application.model.InstallManagerService
import io.eelo.appinstaller.categories.CategoriesFragment
......@@ -22,6 +24,7 @@ import io.eelo.appinstaller.home.HomeFragment
import io.eelo.appinstaller.search.SearchFragment
import io.eelo.appinstaller.settings.SettingsFragment
import io.eelo.appinstaller.updates.UpdatesFragment
import io.eelo.appinstaller.utlis.Constants.STORAGE_PERMISSION_REQUEST_CODE
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemSelectedListener {
......@@ -130,4 +133,10 @@ class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemS
itemView.setChecked(itemView.itemData.isChecked)
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
if (requestCode == STORAGE_PERMISSION_REQUEST_CODE && grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_DENIED) {
Toast.makeText(this, resources.getString(R.string.error_storage_permission_denied), Toast.LENGTH_LONG).show()
}
}
}
package io.eelo.appinstaller.application
import android.Manifest
import android.annotation.SuppressLint
import android.app.Activity
import android.content.pm.PackageManager
import android.os.AsyncTask
import android.support.v7.widget.RecyclerView
import android.view.View
......@@ -15,11 +18,12 @@ import io.eelo.appinstaller.application.model.Downloader
import io.eelo.appinstaller.application.model.State
import io.eelo.appinstaller.application.viewmodel.ApplicationViewModel
import io.eelo.appinstaller.common.ProxyBitmap
import io.eelo.appinstaller.utlis.Constants.STORAGE_PERMISSION_REQUEST_CODE
import kotlinx.android.synthetic.main.application_list_item.view.*
import java.text.DecimalFormat
import kotlin.math.roundToInt
class ApplicationViewHolder(private val view: View) : RecyclerView.ViewHolder(view), ApplicationStateListener {
class ApplicationViewHolder(private val activity: Activity, private val view: View) : RecyclerView.ViewHolder(view), ApplicationStateListener {
private val icon: ImageView = view.app_icon
private val title: TextView = view.app_title
......@@ -37,7 +41,18 @@ class ApplicationViewHolder(private val view: View) : RecyclerView.ViewHolder(vi
applicationViewModel.onApplicationClick(view.context, application!!)
}
}
installButton.setOnClickListener { application?.buttonClicked(view.context) }
installButton.setOnClickListener {
if (android.os.Build.VERSION.SDK_INT >= 23) {
if (activity.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
activity.requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), STORAGE_PERMISSION_REQUEST_CODE)
} else {
application?.buttonClicked(view.context)
}
} else {
application?.buttonClicked(view.context)
}
}
}
fun createApplicationView(app: Application) {
......
package io.eelo.appinstaller.application
import android.Manifest
import android.annotation.SuppressLint
import android.app.Activity
import android.content.pm.PackageManager
import android.os.AsyncTask
import android.support.v7.widget.RecyclerView
import android.view.View
......@@ -14,10 +17,11 @@ import io.eelo.appinstaller.application.model.Downloader
import io.eelo.appinstaller.application.model.State
import io.eelo.appinstaller.application.viewmodel.ApplicationViewModel
import io.eelo.appinstaller.common.ProxyBitmap
import io.eelo.appinstaller.utlis.Constants
import kotlinx.android.synthetic.main.application_list_item.view.*
import kotlin.math.roundToInt
class SmallApplicationViewHolder(private val view: View) : RecyclerView.ViewHolder(view), ApplicationStateListener {
class SmallApplicationViewHolder(private val activity: Activity, private val view: View) : RecyclerView.ViewHolder(view), ApplicationStateListener {
private val icon: ImageView = view.app_icon
private val title: TextView = view.app_title
......@@ -33,7 +37,18 @@ class SmallApplicationViewHolder(private val view: View) : RecyclerView.ViewHold
applicationViewModel.onApplicationClick(view.context, application!!)
}
}
installButton.setOnClickListener { application?.buttonClicked(view.context) }
installButton.setOnClickListener {
if (android.os.Build.VERSION.SDK_INT >= 23) {
if (activity.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
activity.requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), Constants.STORAGE_PERMISSION_REQUEST_CODE)
} else {
application?.buttonClicked(view.context)
}
} else {
application?.buttonClicked(view.context)
}
}
}
fun createApplicationView(app: Application) {
......
......@@ -49,7 +49,7 @@ class CategoryActivity : AppCompatActivity() {
// Initialise recycler view
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = ApplicationListAdapter(applicationList)
recyclerView.adapter = ApplicationListAdapter(this, applicationList)
// Bind to the list of applications in this activity's category
categoriesViewModel.getApplicationsInCategory().observe(this, Observer {
......
package io.eelo.appinstaller.common
import android.app.Activity
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.ViewGroup
......@@ -7,11 +8,11 @@ import io.eelo.appinstaller.R
import io.eelo.appinstaller.application.ApplicationViewHolder
import io.eelo.appinstaller.application.model.Application
class ApplicationListAdapter(private val applicationList: List<Application>) : RecyclerView.Adapter<ApplicationViewHolder>() {
class ApplicationListAdapter(private val activity: Activity, private val applicationList: List<Application>) : RecyclerView.Adapter<ApplicationViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ApplicationViewHolder {
val listItemContainer = LayoutInflater.from(parent.context).inflate(R.layout.application_list_item, parent, false)
return ApplicationViewHolder(listItemContainer)
return ApplicationViewHolder(activity, listItemContainer)
}
override fun onBindViewHolder(holder: ApplicationViewHolder, position: Int) {
......
package io.eelo.appinstaller.common
import android.app.Activity
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.ViewGroup
......@@ -7,11 +8,11 @@ import io.eelo.appinstaller.R
import io.eelo.appinstaller.application.SmallApplicationViewHolder
import io.eelo.appinstaller.application.model.Application
class SmallApplicationListAdapter(private val applicationList: List<Application>) : RecyclerView.Adapter<SmallApplicationViewHolder>() {
class SmallApplicationListAdapter(private val activity: Activity, private val applicationList: List<Application>) : RecyclerView.Adapter<SmallApplicationViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SmallApplicationViewHolder {
val listItemContainer = LayoutInflater.from(parent.context).inflate(R.layout.small_application_list_item, parent, false)
return SmallApplicationViewHolder(listItemContainer)
return SmallApplicationViewHolder(activity, listItemContainer)
}
override fun onBindViewHolder(holder: SmallApplicationViewHolder, position: Int) {
......
package io.eelo.appinstaller.home
import android.app.Activity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
......@@ -12,7 +13,7 @@ import io.eelo.appinstaller.categories.model.Category
import io.eelo.appinstaller.common.SmallApplicationListAdapter
import kotlinx.android.synthetic.main.home_category_list_item.view.*
class HomeCategoryAdapter(private val categoryHashMap: HashMap<Category, ArrayList<Application>>) : RecyclerView.Adapter<HomeCategoryAdapter.HomeCategoryViewHolder>() {
class HomeCategoryAdapter(private val activity: Activity, private val categoryHashMap: HashMap<Category, ArrayList<Application>>) : RecyclerView.Adapter<HomeCategoryAdapter.HomeCategoryViewHolder>() {
private val categoryList = categoryHashMap.keys.toCollection(ArrayList())
......@@ -29,7 +30,7 @@ class HomeCategoryAdapter(private val categoryHashMap: HashMap<Category, ArrayLi
override fun onBindViewHolder(holder: HomeCategoryViewHolder, position: Int) {
holder.title.text = categoryList[position].title
holder.applicationList.layoutManager = LinearLayoutManager(holder.view.context, LinearLayoutManager.HORIZONTAL, false)
holder.applicationList.adapter = SmallApplicationListAdapter(categoryHashMap[categoryList[position]]!!.toList())
holder.applicationList.adapter = SmallApplicationListAdapter(activity, categoryHashMap[categoryList[position]]!!.toList())
}
override fun getItemCount() = categoryHashMap.size
......
......@@ -42,7 +42,7 @@ class HomeFragment : Fragment() {
// Initialise category list
categoryList.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
categoryList.adapter = HomeCategoryAdapter(homeViewModel.getApplications().value!!)
categoryList.adapter = HomeCategoryAdapter(activity!!, homeViewModel.getApplications().value!!)
homeViewModel.loadApplications()
// Bind to the list of images for the carousel
......@@ -57,7 +57,7 @@ class HomeFragment : Fragment() {
// Bind to the list of applications
homeViewModel.getApplications().observe(this, Observer {
categoryList.adapter = HomeCategoryAdapter(homeViewModel.getApplications().value!!)
categoryList.adapter = HomeCategoryAdapter(activity!!, homeViewModel.getApplications().value!!)
})
return view
......
......@@ -62,7 +62,7 @@ class SearchFragment : Fragment(), SearchView.OnQueryTextListener, SearchView.On
// Initialise recycler view
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = LinearLayoutManager(context)
recyclerView.adapter = ApplicationListAdapter(applicationList)
recyclerView.adapter = ApplicationListAdapter(activity!!, applicationList)
// Bind search view suggestions adapter to search suggestions list in view model
searchViewModel.getSuggestions().observe(this, Observer {
......
......@@ -43,6 +43,6 @@ class UpdatesFragment : Fragment() {
private fun initializeRecyclerView(recyclerView: RecyclerView) {
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = LinearLayoutManager(context)
recyclerView.adapter = ApplicationListAdapter(updatesViewModel.getApplications().value!!)
recyclerView.adapter = ApplicationListAdapter(activity!!, updatesViewModel.getApplications().value!!)
}
}
\ No newline at end of file
......@@ -5,6 +5,7 @@ object Constants {
// Global
const val BASE_URL = "https://api.cleanapk.org/"
const val DOWNLOAD_URL = "https://apk.cleanapk.org/"
const val STORAGE_PERMISSION_REQUEST_CODE = 0
// Search
const val RESULTS_PER_PAGE = 20
......
......@@ -17,6 +17,7 @@
<string name="app_privacy_score">Privacy</string>
<string name="app_rating">Rating</string>
<string name="app_energy_score">Energy</string>
<string name="error_storage_permission_denied">Storage access is required for downloading apps</string>
<!-- Bottom Navigation Bar -->
<string name="menu_home">Home</string>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment