Warning, /network/kdeconnect-android/src/org/kde/kdeconnect/UserInterface/About/LicensesActivity.kt is written in an unsupported language. File is not indexed.

0001 /*
0002  * SPDX-FileCopyrightText: 2021 Maxim Leshchenko <cnmaks90@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 package org.kde.kdeconnect.UserInterface.About
0008 
0009 import android.os.Bundle
0010 import android.util.DisplayMetrics
0011 import android.view.Menu
0012 import android.view.MenuItem
0013 import androidx.appcompat.app.AppCompatActivity
0014 import androidx.recyclerview.widget.LinearLayoutManager
0015 import androidx.recyclerview.widget.LinearSmoothScroller
0016 import org.apache.commons.io.IOUtils
0017 import org.kde.kdeconnect_tp.R
0018 import org.kde.kdeconnect_tp.databinding.ActivityLicensesBinding
0019 import java.nio.charset.Charset
0020 
0021 class LicensesActivity : AppCompatActivity() {
0022     private lateinit var binding: ActivityLicensesBinding
0023 
0024     override fun onCreate(savedInstanceState: Bundle?) {
0025         super.onCreate(savedInstanceState)
0026 
0027         binding = ActivityLicensesBinding.inflate(layoutInflater)
0028         setContentView(binding.root)
0029 
0030         setSupportActionBar(binding.toolbarLayout.toolbar)
0031         supportActionBar!!.setDisplayHomeAsUpEnabled(true)
0032         supportActionBar!!.setDisplayShowHomeEnabled(true)
0033 
0034         binding.licensesText.layoutManager = LinearLayoutManager(this)
0035         binding.licensesText.adapter = StringListAdapter(getLicenses().split("\n\n"))
0036     }
0037 
0038     private fun getLicenses(): String = resources.openRawResource(R.raw.license).use { inputStream -> IOUtils.toString(inputStream, Charset.defaultCharset()) }
0039 
0040     override fun onCreateOptionsMenu(menu: Menu): Boolean {
0041         menuInflater.inflate(R.menu.menu_licenses, menu)
0042         return super.onCreateOptionsMenu(menu)
0043     }
0044 
0045     private fun smoothScrollToPosition(position: Int) {
0046         val linearSmoothScroller: LinearSmoothScroller = object : LinearSmoothScroller(this) {
0047             override fun calculateSpeedPerPixel(displayMetrics: DisplayMetrics): Float = 2.5F / displayMetrics.densityDpi
0048         }
0049 
0050         linearSmoothScroller.targetPosition = position
0051         binding.licensesText.layoutManager?.startSmoothScroll(linearSmoothScroller)
0052     }
0053 
0054     override fun onOptionsItemSelected(item: MenuItem): Boolean = when (item.itemId) {
0055         R.id.menu_rise_up -> {
0056             smoothScrollToPosition(0)
0057             true
0058         }
0059         R.id.menu_rise_down -> {
0060             smoothScrollToPosition(binding.licensesText.adapter!!.itemCount - 1)
0061             true
0062         }
0063         else -> super.onOptionsItemSelected(item)
0064     }
0065 
0066     override fun onSupportNavigateUp(): Boolean {
0067         super.onBackPressed()
0068         return true
0069     }
0070 }