Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mm.basiceducation.beprimary.activity
- import android.Manifest
- import android.app.DownloadManager
- import android.app.ProgressDialog
- import android.content.Context
- import android.content.Intent
- import android.content.pm.PackageManager
- import android.net.Uri
- import android.os.Bundle
- import android.os.Environment
- import android.view.View
- import android.widget.TextView
- import android.widget.Toast
- import androidx.appcompat.app.AppCompatActivity
- import androidx.core.app.ActivityCompat
- import androidx.core.content.ContextCompat
- import androidx.recyclerview.widget.RecyclerView
- import com.google.android.gms.ads.*
- import com.google.firebase.firestore.FirebaseFirestore
- import mm.basiceducation.beprimary.R
- import mm.basiceducation.beprimary.adapter.ChapterAdapter
- import mm.basiceducation.beprimary.dto.Chapter
- import mm.basiceducation.beprimary.listener.ClickListener
- import java.io.File
- class ChapterActivity : AppCompatActivity() {
- lateinit var mAdView: AdView
- private lateinit var mInterstitialAd: InterstitialAd
- companion object {
- private const val REQUEST_CODE_PERMISSION_STORAGE = 100
- }
- lateinit var grade: String
- lateinit var subject: String
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_chapter)
- //request permission
- if (!checkPermission()) {
- requestPermission()
- }
- val db = FirebaseFirestore.getInstance()
- val data = mutableListOf<Chapter>()
- val text = findViewById<TextView>(R.id.chapter_text)
- val recyclerView = findViewById<RecyclerView>(R.id.chatper_recyclerview)
- //banner
- MobileAds.initialize(this)
- mAdView = findViewById(R.id.chapter_adview)
- val adRequest = AdRequest.Builder().build()
- mAdView.loadAd(adRequest)
- //interstitial
- mInterstitialAd = InterstitialAd(this)
- mInterstitialAd.adUnitId = "ca-app-pub-3940256099942544/1033173712"
- mInterstitialAd.loadAd(AdRequest.Builder().build())
- grade = intent?.getStringExtra("grade") as String
- val user = intent?.getStringExtra("user")
- subject = intent?.getStringExtra("subject") as String
- supportActionBar?.title = "$grade $subject"
- db.collection("chapter")
- .whereEqualTo("subject_id", intent.getIntExtra("subjectid", 0))
- .get()
- .addOnCompleteListener { task ->
- if (task.isSuccessful) {
- for (document in task.result!!) {
- data.add(document.toObject(Chapter::class.java))
- }
- if (data.size < 1) {
- text.visibility = View.VISIBLE
- }
- data.sort()
- val adapter = ChapterAdapter(data, applicationContext)
- recyclerView.adapter = adapter
- adapter.setClickListener(ClickListener { _, positon ->
- if (checkPermission()) {
- downloadPDF(data[positon].link, "$grade $user $subject ${data[positon].description}.pdf")
- } else {
- requestPermission()
- Toast.makeText(applicationContext, "Need Permission", Toast.LENGTH_SHORT).show()
- }
- })
- }
- }
- }
- fun downloadPDF(link: String, name: String) {
- // two hour to find this single line of code
- val path = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), name)
- if (!path.exists()) {
- // Toast.makeText(applicationContext, path.toString(), Toast.LENGTH_SHORT).show()
- val uri = Uri.parse(link)
- val request = DownloadManager.Request(uri)
- request.setTitle(name)
- request.setDescription(name)
- request.setVisibleInDownloadsUi(true)
- request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
- request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, name)
- val downloadManager =
- getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
- val downloadid = downloadManager.enqueue(request)
- val progressBarDialog = ProgressDialog(this)
- progressBarDialog.setTitle("Loading, Please Wait")
- // progressBarDialog.setTitle(name)
- progressBarDialog.setCancelable(false)
- progressBarDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL)
- progressBarDialog.progress = 0
- Thread(Runnable {
- var downloading = true
- val manager =
- this.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
- while (downloading) {
- val q = DownloadManager.Query()
- q.setFilterById(downloadid) //filter by id which you have receieved when reqesting download from download manager
- val cursor = manager.query(q)
- cursor.moveToFirst()
- val bytesDownloaded = cursor.getInt(
- cursor
- .getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)
- )
- val bytes_total =
- cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES))
- if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
- progressBarDialog.dismiss()
- downloading = false
- }
- val dl_progress = (bytesDownloaded * 100L / bytes_total).toInt()
- runOnUiThread { progressBarDialog.progress = dl_progress as Int }
- // Log.d(Constants.MAIN_VIEW_ACTIVITY, statusMessage(cursor));
- cursor.close()
- }
- }).start()
- progressBarDialog.show()
- } else {
- val intent = Intent(this, DetailActivity::class.java)
- intent.putExtra("PDF", name)
- intent.putExtra("name", "$grade $subject")
- if (mInterstitialAd.isLoaded) {
- mInterstitialAd.show()
- mInterstitialAd.adListener = object : AdListener() {
- override fun onAdClosed() {
- super.onAdClosed()
- mInterstitialAd.loadAd(AdRequest.Builder().build())
- startActivity(intent)
- }
- }
- } else {
- startActivity(intent)
- }
- }
- }
- private fun checkPermission(): Boolean {
- return ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
- }
- private fun requestPermission() {
- ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), REQUEST_CODE_PERMISSION_STORAGE)
- }
- override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
- if (requestCode == REQUEST_CODE_PERMISSION_STORAGE) {
- if (grantResults.isEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- } else if (grantResults.isEmpty() && grantResults[0] == PackageManager.PERMISSION_DENIED) {
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement