导航菜单

页面标题

页面副标题

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

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

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


package com.mcb.stjohnsemschool.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.google.firebase.analytics.FirebaseAnalytics;
import com.mcb.stjohnsemschool.interfaces.ExitEvent;
import com.mcb.stjohnsemschool.model.FilePathModelClass;
import com.mcb.stjohnsemschool.utils.TouchImageView;
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.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

public class GalleryImageViewPagerActivity extends AppCompatActivity {
    private Activity activity;
    private MenuItem downloadItem;
    ArrayList<FilePathModelClass> filePathModelClasses;
    Intent intent;
    CustomPagerAdapter mCustomPagerAdapter;
    FirebaseAnalytics mFirebaseAnalytics;
    private TransparentProgressDialog mProgressbar;
    ViewPager mViewPager;
    int selectedPosition;

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_gallery_image_view_pager);
        this.activity = this;
        this.mProgressbar = new TransparentProgressDialog(this, R.drawable.spinner_loading_imag);
        this.mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        Intent intent = getIntent();
        this.intent = intent;
        this.filePathModelClasses = intent.getParcelableArrayListExtra("array");
        setTitle(this.intent.getStringExtra("title"));
        this.selectedPosition = this.intent.getIntExtra("position", 0);
        this.mCustomPagerAdapter = new CustomPagerAdapter(this, this.filePathModelClasses);
        ViewPager findViewById = findViewById(R.id.pager);
        this.mViewPager = findViewById;
        findViewById.setAdapter(this.mCustomPagerAdapter);
        this.mViewPager.setCurrentItem(this.selectedPosition);
        this.mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            public void onPageScrollStateChanged(int i) {
            }

            public void onPageScrolled(int i, float f, int i2) {
            }

            public void onPageSelected(int i) {
                if (GalleryImageViewPagerActivity.this.downloadItem != null) {
                    String filePath = GalleryImageViewPagerActivity.this.filePathModelClasses.get(i).getFilePath();
                    if (new File(Utility.getDownloadStorageDir(GalleryImageViewPagerActivity.this.getApplicationContext()) + "/MyClassBoard/" + filePath.substring(filePath.lastIndexOf("/") + 1)).exists()) {
                        GalleryImageViewPagerActivity.this.downloadItem.setVisible(false);
                    } else {
                        GalleryImageViewPagerActivity.this.downloadItem.setVisible(true);
                    }
                }
            }
        });
    }

    class CustomPagerAdapter extends PagerAdapter {
        Context mContext;
        ArrayList<FilePathModelClass> mFilePathModelClasses;
        LayoutInflater mLayoutInflater;
        ProgressBar progressBar;

        public CustomPagerAdapter(Context context, ArrayList<FilePathModelClass> arrayList) {
            this.mContext = context;
            this.mLayoutInflater = (LayoutInflater) context.getSystemService("layout_inflater");
            this.mFilePathModelClasses = arrayList;
        }

        public int getCount() {
            return this.mFilePathModelClasses.size();
        }

        public boolean isViewFromObject(View view, Object obj) {
            return view == ((FrameLayout) obj);
        }

        public Object instantiateItem(ViewGroup viewGroup, final int i) {
            View inflate = this.mLayoutInflater.inflate(R.layout.gallery_view_pager_item_layout, viewGroup, false);
            TouchImageView findViewById = inflate.findViewById(2131362629);
            ImageView imageView = (ImageView) inflate.findViewById(R.id.playImg);
            this.progressBar = (ProgressBar) inflate.findViewById(R.id.progress_bar);
            this.mFilePathModelClasses.get(i).setpBar(this.progressBar);
            this.mFilePathModelClasses.get(i).getpBar().setVisibility(0);
            findViewById.setTag(R.id.glide_tag, Integer.valueOf(i));
            findViewById.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    FilePathModelClass filePathModelClass = GalleryImageViewPagerActivity.this.filePathModelClasses.get(((Integer) view.getTag(R.id.glide_tag)).intValue());
                    String filePath = filePathModelClass.getFilePath();
                    if (filePath != null && filePath.length() > 0 && !filePath.equalsIgnoreCase("null")) {
                        if (filePathModelClass.getAttachmentType() == 2) {
                            if (filePath.contains("vimeo")) {
                                Intent intent = new Intent(CustomPagerAdapter.this.mContext, (Class<?>) LearningVideoWebViewActivity.class);
                                intent.addFlags(268435456);
                                intent.putExtra("URL", filePath);
                                intent.putExtra("Title", filePathModelClass.getDisplayFileName() == null ? filePathModelClass.getFileName() : filePathModelClass.getDisplayFileName());
                                CustomPagerAdapter.this.mContext.startActivity(intent);
                                return;
                            }
                            Intent intent2 = new Intent(GalleryImageViewPagerActivity.this.getApplicationContext(), (Class<?>) LearningVideoActivity.class);
                            intent2.putExtra("VIDEO_URL", filePath);
                            GalleryImageViewPagerActivity.this.startActivity(intent2);
                            return;
                        }
                        if (filePath.toLowerCase().endsWith(".gif") || filePath.toLowerCase().endsWith(".jpg") || filePath.toLowerCase().endsWith(".jpeg") || filePath.toLowerCase().endsWith(".png") || filePath.toLowerCase().endsWith(".heic") || filePath.toLowerCase().endsWith(".heif")) {
                            return;
                        }
                        try {
                            Intent intent3 = new Intent("android.intent.action.VIEW", Uri.parse(filePath));
                            intent3.addFlags(268435456);
                            CustomPagerAdapter.this.mContext.startActivity(intent3);
                            return;
                        } catch (Exception e) {
                            e.printStackTrace();
                            return;
                        }
                    }
                    Toast.makeText(CustomPagerAdapter.this.mContext, "Image not available", 0).show();
                }
            });
            String filePath = this.mFilePathModelClasses.get(i).getFilePath();
            int attachmentType = this.mFilePathModelClasses.get(i).getAttachmentType();
            if (filePath == null || filePath.length() <= 0 || filePath.equalsIgnoreCase("null")) {
                if (imageView != null) {
                    imageView.setVisibility(8);
                }
                findViewById.setImageResource(R.drawable.noimage);
                this.mFilePathModelClasses.get(i).getpBar().setVisibility(8);
            } else if (attachmentType == 2) {
                if (imageView != null) {
                    imageView.setVisibility(0);
                }
                Glide.with(this.mContext).load(Utility.getThumbnailFromYoutubeURL(filePath)).apply(new RequestOptions().placeholder(R.drawable.image_link_icon)).listener(new RequestListener<Drawable>() {
                    public boolean onResourceReady(Object obj, Object obj2, Target target, DataSource dataSource, boolean z) {
                        return onResourceReady((Drawable) obj, obj2, (Target<Drawable>) target, dataSource, z);
                    }

                    public boolean onLoadFailed(GlideException glideException, Object obj, Target<Drawable> target, boolean z) {
                        CustomPagerAdapter.this.mFilePathModelClasses.get(i).getpBar().setVisibility(8);
                        return false;
                    }

                    public boolean onResourceReady(Drawable drawable, Object obj, Target<Drawable> target, DataSource dataSource, boolean z) {
                        CustomPagerAdapter.this.mFilePathModelClasses.get(i).getpBar().setVisibility(8);
                        return false;
                    }
                }).into(findViewById);
            } else {
                if (imageView != null) {
                    imageView.setVisibility(8);
                }
                if (filePath.toLowerCase().endsWith(".gif") || filePath.toLowerCase().endsWith(".jpg") || filePath.toLowerCase().endsWith(".jpeg") || filePath.toLowerCase().endsWith(".png") || filePath.toLowerCase().endsWith(".heic") || filePath.toLowerCase().endsWith(".heif")) {
                    Glide.with(this.mContext).load(filePath).apply(new RequestOptions().placeholder(R.drawable.loadingbar2)).listener(new RequestListener<Drawable>() {
                        public boolean onResourceReady(Object obj, Object obj2, Target target, DataSource dataSource, boolean z) {
                            return onResourceReady((Drawable) obj, obj2, (Target<Drawable>) target, dataSource, z);
                        }

                        public boolean onLoadFailed(GlideException glideException, Object obj, Target<Drawable> target, boolean z) {
                            CustomPagerAdapter.this.mFilePathModelClasses.get(i).getpBar().setVisibility(8);
                            return false;
                        }

                        public boolean onResourceReady(Drawable drawable, Object obj, Target<Drawable> target, DataSource dataSource, boolean z) {
                            CustomPagerAdapter.this.mFilePathModelClasses.get(i).getpBar().setVisibility(8);
                            return false;
                        }
                    }).into(findViewById);
                } else {
                    this.mFilePathModelClasses.get(i).getpBar().setVisibility(8);
                    findViewById.setImageResource(R.drawable.image_link_icon);
                }
            }
            viewGroup.addView(inflate);
            return inflate;
        }

        public void destroyItem(ViewGroup viewGroup, int i, Object obj) {
            viewGroup.removeView((FrameLayout) obj);
        }
    }

    public void onBackPressed() {
        super.onBackPressed();
        new ExitEvent().onTaskRemoved(new Intent((Context) this, (Class<?>) GalleryImageViewPagerActivity.class));
    }

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

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

    public boolean download(MenuItem menuItem) {
        String filePath = this.filePathModelClasses.get(this.mViewPager.getCurrentItem()).getFilePath();
        if (Utility.hasNetworkConnection(getApplicationContext())) {
            new DownloadFileAsync(filePath).execute(new String[0]);
            return true;
        }
        Utility.showInfoDialog(this.activity, "Please check your internet connection");
        return true;
    }

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

        @Override
        public void onProgressUpdate(String... strArr) {
        }

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

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if (GalleryImageViewPagerActivity.this.mProgressbar == null || GalleryImageViewPagerActivity.this.mProgressbar.isShowing()) {
                return;
            }
            GalleryImageViewPagerActivity.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(GalleryImageViewPagerActivity.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 (GalleryImageViewPagerActivity.this.mProgressbar != null && GalleryImageViewPagerActivity.this.mProgressbar.isShowing()) {
                GalleryImageViewPagerActivity.this.mProgressbar.dismiss();
            }
            String str2 = this.imageUrl;
            File file = new File(Utility.getDownloadStorageDir(GalleryImageViewPagerActivity.this.getApplicationContext()) + "/MyClassBoard/" + str2.substring(str2.lastIndexOf("/") + 1));
            if (file.exists()) {
                GalleryImageViewPagerActivity.this.downloadItem.setVisible(false);
            } else {
                GalleryImageViewPagerActivity.this.downloadItem.setVisible(true);
            }
            GalleryImageViewPagerActivity.addToGallery(GalleryImageViewPagerActivity.this.getApplicationContext(), file.getAbsolutePath());
            Utility.showInfoDialog(GalleryImageViewPagerActivity.this.activity, "Image downloaded to " + file.getAbsolutePath());
        }
    }

    public static void addToGallery(Context context, String str) {
        Intent intent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
        intent.setData(Uri.fromFile(new File(str)));
        context.sendBroadcast(intent);
    }

    public boolean share(MenuItem menuItem) {
        shareImage(this.filePathModelClasses.get(this.mViewPager.getCurrentItem()).getFilePath(), this);
        return true;
    }

    public void shareImage(final String str, final Context context) {
        new AsyncTask<Void, Void, Bitmap>() {
            private TransparentProgressDialog mProgressbar;

            @Override
            protected void onPreExecute() {
                TransparentProgressDialog transparentProgressDialog = new TransparentProgressDialog(context, R.drawable.spinner_loading_imag);
                this.mProgressbar = transparentProgressDialog;
                transparentProgressDialog.show();
            }

            @Override
            public Bitmap doInBackground(Void... voidArr) {
                try {
                    return BitmapFactory.decodeStream(new URL(str).openConnection().getInputStream());
                } catch (IOException e) {
                    System.out.println(e);
                    return null;
                }
            }

            @Override
            public void onPostExecute(Bitmap bitmap) {
                TransparentProgressDialog transparentProgressDialog = this.mProgressbar;
                if (transparentProgressDialog != null && transparentProgressDialog.isShowing()) {
                    this.mProgressbar.dismiss();
                }
                if (bitmap == null) {
                    Utility.showInfoDialog(GalleryImageViewPagerActivity.this.activity, "Failed to share! Check your data connection");
                    return;
                }
                Intent intent = new Intent("android.intent.action.SEND");
                intent.addFlags(402653184);
                intent.addFlags(1);
                intent.setType("image*//*");
                intent.putExtra("android.intent.extra.STREAM", GalleryImageViewPagerActivity.getLocalBitmapUri(bitmap, context));
                context.startActivity(Intent.createChooser(intent, "Share Image"));
            }
        }.execute(new Void[0]);
    }

    public static Uri getLocalBitmapUri(Bitmap bitmap, Context context) {
        try {
            File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, fileOutputStream);
            fileOutputStream.close();
            return FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".fileprovider", file);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    protected void onResume() {
        super.onResume();
        String string = getSharedPreferences("", 0).getString("usernamekey", "");
        Bundle bundle = new Bundle();
        bundle.putString("user_name", string);
        FirebaseAnalytics.getInstance(this).logEvent("PAGE_GALLERY_IMAGE_PAGER", bundle);
    }

    protected void onDestroy() {
        super.onDestroy();
        new ExitEvent().onTaskRemoved(new Intent((Context) this, (Class<?>) GalleryImageViewPagerActivity.class));
    }
}