app/src/main/java/com/equwal/sbm/MoreAppsActivity.kt (2491 bytes)
1 package com.equwal.sbm 2 3 import android.app.Activity 4 import android.content.ActivityNotFoundException 5 import android.content.Intent 6 import android.net.Uri 7 import android.os.Build 8 import android.os.Bundle 9 import android.view.View 10 import android.view.ViewGroup 11 import android.view.WindowInsets 12 import android.widget.BaseAdapter 13 import android.widget.ListView 14 import android.widget.TextView 15 import android.widget.Toast 16 import android.widget.Toolbar 17 18 /** The other sites and apps of the same author, from the menu of the list. A tap opens the page in a browser. */ 19 class MoreAppsActivity : Activity() { 20 override fun onCreate(state: Bundle?) { 21 super.onCreate(state) 22 setContentView(R.layout.activity_more_apps) 23 val toolbar = findViewById<Toolbar>(R.id.toolbar) 24 setActionBar(toolbar) 25 26 // The same as the list screen: the toolbar colour goes under the 27 // status bar, and the rest of the screen stays clear of the bars. 28 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { 29 findViewById<View>(R.id.root).setOnApplyWindowInsetsListener { v, insets -> 30 val bars = insets.getInsets(WindowInsets.Type.systemBars()) 31 toolbar.setPadding(0, bars.top, 0, 0) 32 v.setPadding(bars.left, 0, bars.right, bars.bottom) 33 insets 34 } 35 } 36 37 val list = findViewById<ListView>(R.id.list) 38 list.adapter = Rows() 39 list.setOnItemClickListener { _, _, position, _ -> open(MORE_APPS[position].url) } 40 } 41 42 private fun open(url: String) { 43 try { 44 startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)).addCategory(Intent.CATEGORY_BROWSABLE)) 45 } catch (e: ActivityNotFoundException) { 46 Toast.makeText(this, getString(R.string.no_app, url), Toast.LENGTH_LONG).show() 47 } 48 } 49 50 private inner class Rows : BaseAdapter() { 51 override fun getCount() = MORE_APPS.size 52 override fun getItem(position: Int) = MORE_APPS[position] 53 override fun getItemId(position: Int) = position.toLong() 54 55 override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { 56 val view = convertView ?: layoutInflater.inflate(R.layout.item_more_app, parent, false) 57 view.findViewById<TextView>(R.id.title).setText(MORE_APPS[position].name) 58 view.findViewById<TextView>(R.id.detail).setText(MORE_APPS[position].line) 59 return view 60 } 61 } 62 }