导航菜单

页面标题

页面副标题

St.John's v1.0.9 - LearningAudioActivity.java 源代码

正在查看: St.John's v1.0.9 应用的 LearningAudioActivity.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.mcb.stjohnsemschool.activity;

import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.mcb.stjohnsemschool.utils.TransparentProgressDialog;
import com.mcb.stjohnsemschool.utils.Utility;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLConnection;

public class LearningAudioActivity extends AppCompatActivity {
    private boolean allowDownload;
    private int chapterId;
    private MenuItem downloadItem;
    private boolean isLearning;
    private TextView mAudioFileName;
    private ImageView mCloseAudio;
    private ImageView mPauseAudio;
    private ImageView mPlayAudio;
    private MediaPlayer mPlayer;
    private TransparentProgressDialog mProgressbar;
    private SeekBar mSBar;
    private int resourceId;
    private int topicId;
    private int oTime = 0;
    private int sTime = 0;
    private int eTime = 0;
    private Handler hdlr = new Handler();
    private String subjectGUID = "";
    private String url = "";
    private Runnable UpdateTime = new Runnable() {
        @Override
        public void run() {
            if (LearningAudioActivity.this.mPlayer == null || !LearningAudioActivity.this.mPlayer.isPlaying()) {
                return;
            }
            LearningAudioActivity learningAudioActivity = LearningAudioActivity.this;
            learningAudioActivity.sTime = learningAudioActivity.mPlayer.getCurrentPosition();
            LearningAudioActivity.this.mSBar.setProgress(LearningAudioActivity.this.sTime);
            LearningAudioActivity.this.hdlr.postDelayed(this, 100L);
        }
    };

    public boolean share(MenuItem menuItem) {
        return true;
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_audio_play);
        Bundle extras = getIntent().getExtras();
        this.isLearning = extras.getBoolean("IsLearning", false);
        this.allowDownload = extras.getBoolean("AllowDownload", false);
        if (this.isLearning) {
            this.subjectGUID = extras.getString("SubjectGUID", "");
            this.chapterId = extras.getInt("ChapterId", 0);
            this.topicId = extras.getInt("TopicId", 0);
            this.resourceId = extras.getInt("ResourceId", 0);
        }
        this.mProgressbar = new TransparentProgressDialog(this, R.drawable.spinner_loading_imag);
        setupUI();
    }

    private void setupUI() {
        this.mAudioFileName = (TextView) findViewById(R.id.txv_file_name);
        this.mPlayAudio = (ImageView) findViewById(R.id.btn_play);
        this.mPauseAudio = (ImageView) findViewById(R.id.btn_pause);
        this.mCloseAudio = (ImageView) findViewById(R.id.img_close);
        this.mSBar = (SeekBar) findViewById(R.id.sbar);
        this.mCloseAudio.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LearningAudioActivity.this.mPlayer.stop();
                LearningAudioActivity.this.mPlayer.release();
                LearningAudioActivity.this.mPlayer = null;
            }
        });
        this.mPlayAudio.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LearningAudioActivity.this.startPlayer();
            }
        });
        this.mPauseAudio.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LearningAudioActivity.this.mPlayer.pause();
                LearningAudioActivity.this.mPauseAudio.setVisibility(8);
                LearningAudioActivity.this.mPlayAudio.setVisibility(0);
            }
        });
        this.mSBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean z) {
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if (LearningAudioActivity.this.mPlayer == null || !LearningAudioActivity.this.mPlayer.isPlaying()) {
                    return;
                }
                LearningAudioActivity.this.mPlayer.seekTo(seekBar.getProgress());
            }
        });
        String stringExtra = getIntent().getStringExtra("Title");
        this.url = getIntent().getStringExtra("URL");
        getSupportActionBar().setTitle(stringExtra);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        this.mAudioFileName.setText(stringExtra);
        try {
            MediaPlayer mediaPlayer = new MediaPlayer();
            this.mPlayer = mediaPlayer;
            mediaPlayer.setDataSource(this.url);
            this.mPlayer.prepare();
            startPlayer();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void startPlayer() {
        this.mPlayer.start();
        this.eTime = this.mPlayer.getDuration();
        this.sTime = this.mPlayer.getCurrentPosition();
        if (this.oTime == 0) {
            this.mSBar.setMax(this.eTime);
            this.oTime = 1;
        }
        this.mSBar.setProgress(this.sTime);
        this.hdlr.postDelayed(this.UpdateTime, 100L);
        this.mPauseAudio.setVisibility(0);
        this.mPlayAudio.setVisibility(8);
    }

    public boolean onOptionsItemSelected(MenuItem menuItem) {
        if (menuItem.getItemId() == 16908332) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(menuItem);
    }

    public void onBackPressed() {
        MediaPlayer mediaPlayer = this.mPlayer;
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            this.mPlayer.release();
            this.mPlayer = null;
        }
        super.onBackPressed();
    }

    protected void onDestroy() {
        if (this.isLearning) {
            Utility.saveFileViewLog(getApplicationContext(), this.subjectGUID, this.chapterId, this.topicId, this.resourceId, 1);
        }
        super.onDestroy();
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.download_share_main, menu);
        this.downloadItem = menu.findItem(R.id.action_download);
        menu.findItem(R.id.action_share).setVisible(false);
        MenuItem menuItem = this.downloadItem;
        if (menuItem != null) {
            if (this.allowDownload) {
                String str = this.url;
                if (new File(Utility.getDownloadStorageDir(getApplicationContext()) + "/MyClassBoard/" + str.substring(str.lastIndexOf("/") + 1)).exists()) {
                    this.downloadItem.setVisible(false);
                } else {
                    this.downloadItem.setVisible(true);
                }
            } else {
                menuItem.setVisible(false);
            }
        }
        return true;
    }

    public boolean download(MenuItem menuItem) {
        downloadFile();
        return true;
    }

    public void download(View view) {
        downloadFile();
    }

    public void downloadFile() {
        if (Utility.hasNetworkConnection(getApplicationContext())) {
            new DownloadFileAsync(this.url).execute(new String[0]);
        } else {
            Utility.showInfoDialog(this, "Please check your internet connection");
        }
    }

    class DownloadFileAsync extends AsyncTask<String, String, String> {
        private String imageUrl;

        public DownloadFileAsync(String str) {
            this.imageUrl = str;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if (LearningAudioActivity.this.mProgressbar == null || LearningAudioActivity.this.mProgressbar.isShowing()) {
                return;
            }
            LearningAudioActivity.this.mProgressbar.show();
        }

        @Override
        public String doInBackground(String... strArr) {
            try {
                try {
                    URL url = new URL(this.imageUrl);
                    URLConnection openConnection = url.openConnection();
                    openConnection.connect();
                    int contentLength = openConnection.getContentLength();
                    File file = new File(Utility.getDownloadStorageDir(LearningAudioActivity.this.getApplicationContext()) + "/MyClassBoard/");
                    if (!file.exists()) {
                        file.mkdirs();
                    }
                    String str = this.imageUrl;
                    String substring = str.substring(str.lastIndexOf("/") + 1);
                    BufferedInputStream bufferedInputStream = new BufferedInputStream(url.openStream());
                    FileOutputStream fileOutputStream = new FileOutputStream(file + "/" + substring);
                    byte[] bArr = new byte[1024];
                    long j = 0;
                    while (true) {
                        int read = bufferedInputStream.read(bArr);
                        if (read != -1) {
                            j += read;
                            long j2 = (100 * j) / contentLength;
                            fileOutputStream.write(bArr, 0, read);
                        } else {
                            fileOutputStream.flush();
                            fileOutputStream.close();
                            bufferedInputStream.close();
                            return null;
                        }
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    return null;
                }
            } catch (Exception e2) {
                e2.printStackTrace();
                return null;
            }
        }

        @Override
        public void onPostExecute(String str) {
            if (LearningAudioActivity.this.mProgressbar != null && LearningAudioActivity.this.mProgressbar.isShowing()) {
                LearningAudioActivity.this.mProgressbar.dismiss();
            }
            String str2 = this.imageUrl;
            File file = new File(Utility.getDownloadStorageDir(LearningAudioActivity.this.getApplicationContext()) + "/MyClassBoard/" + str2.substring(str2.lastIndexOf("/") + 1));
            if (file.exists()) {
                LearningAudioActivity.this.downloadItem.setVisible(false);
            } else {
                LearningAudioActivity.this.downloadItem.setVisible(true);
            }
            Utility.showInfoDialog(LearningAudioActivity.this, "Downloaded to " + file.getAbsolutePath());
        }
    }
}