diff options
author | Alex Ning <chineseperson5@gmail.com> | 2019-10-08 13:48:58 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2019-10-08 13:48:58 +0000 |
commit | c4fd313839466df975a2afc6ad03e8f06dedf2e8 (patch) | |
tree | fda8615753caa54f02b45ff189c858e71bc89b34 /app | |
parent | 35f9a645d864922f12fa9c04793adf7e3ce39036 (diff) | |
download | infinity-for-reddit-c4fd313839466df975a2afc6ad03e8f06dedf2e8.tar infinity-for-reddit-c4fd313839466df975a2afc6ad03e8f06dedf2e8.tar.gz infinity-for-reddit-c4fd313839466df975a2afc6ad03e8f06dedf2e8.tar.bz2 infinity-for-reddit-c4fd313839466df975a2afc6ad03e8f06dedf2e8.tar.lz infinity-for-reddit-c4fd313839466df975a2afc6ad03e8f06dedf2e8.tar.xz infinity-for-reddit-c4fd313839466df975a2afc6ad03e8f06dedf2e8.tar.zst infinity-for-reddit-c4fd313839466df975a2afc6ad03e8f06dedf2e8.zip |
Add ViewGifActivity to view gifs. Fixed some links cannot be opened by browsers. Maybe fixed IllegalStateException in ViewSubredditDetailActivity and FilteredThingActivity. Minor bugs fixed.
Diffstat (limited to 'app')
17 files changed, 575 insertions, 60 deletions
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index cfddbb91..907caafe 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,15 +2,6 @@ <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="ml.docilealligator.infinityforreddit"> - <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> - <uses-permission android:name="android.permission.INTERNET" /> - - <uses-permission - android:name="android.permission.WRITE_EXTERNAL_STORAGE" - android:maxSdkVersion="22" /> - - <uses-permission-sdk-23 android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> - <application android:name=".Infinity" android:allowBackup="true" @@ -21,6 +12,10 @@ android:theme="@style/AppTheme" android:usesCleartextTraffic="true"> <activity + android:name=".Activity.ViewGIFActivity" + android:parentActivityName=".Activity.MainActivity" + android:theme="@style/AppTheme.ActionBar.Transparent" /> + <activity android:name=".Activity.AccountSavedThingActivity" android:label="@string/account_saved_thing_activity" android:parentActivityName=".Activity.MainActivity" @@ -258,5 +253,13 @@ android:enabled="true" android:exported="false" /> </application> + <uses-permission android:name="android.permission.INTERNET" /> + <uses-permission + android:name="android.permission.WRITE_EXTERNAL_STORAGE" + android:maxSdkVersion="22" /> + + <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> + + <uses-permission-sdk-23 android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> </manifest>
\ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/FilteredThingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/FilteredThingActivity.java index 28e6dd20..52f370b2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/FilteredThingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/FilteredThingActivity.java @@ -148,11 +148,15 @@ public class FilteredThingActivity extends BaseActivity implements SortTypeBotto isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE); mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); + + mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE); + getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_filtered_posts_activity, mFragment).commit(); + if (!mNullAccessToken && mAccessToken == null) { getCurrentAccountAndBindView(filter, sortType); } else { - mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE); - getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_filtered_posts_activity, mFragment).commit(); + /*mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE); + getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_filtered_posts_activity, mFragment).commit();*/ bindView(filter, sortType, false); } } else { @@ -236,7 +240,7 @@ public class FilteredThingActivity extends BaseActivity implements SortTypeBotto case Post.VIDEO_TYPE: toolbar.setSubtitle(R.string.video); break; - case Post.GIF_VIDEO_TYPE: + case Post.GIF_TYPE: toolbar.setSubtitle(R.string.gif); } @@ -333,9 +337,7 @@ public class FilteredThingActivity extends BaseActivity implements SortTypeBotto @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); - if (mFragment != null) { - getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment); - } + getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment); outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode); outState.putString(ACCESS_TOKEN_STATE, mAccessToken); outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LinkResolverActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LinkResolverActivity.java index c35a8073..b85d14f2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LinkResolverActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LinkResolverActivity.java @@ -165,7 +165,6 @@ public class LinkResolverActivity extends AppCompatActivity { } if (!packageNames.isEmpty()) { - intent.setPackage(packageNames.get(0)); try { startActivity(intent); } catch (ActivityNotFoundException e) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewGIFActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewGIFActivity.java new file mode 100644 index 00000000..be842ae0 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewGIFActivity.java @@ -0,0 +1,448 @@ +package ml.docilealligator.infinityforreddit.Activity; + +import android.Manifest; +import android.animation.Animator; +import android.animation.ArgbEvaluator; +import android.animation.ValueAnimator; +import android.app.DownloadManager; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.content.pm.PackageManager; +import android.graphics.PorterDuff; +import android.graphics.drawable.ColorDrawable; +import android.graphics.drawable.Drawable; +import android.net.Uri; +import android.os.Build; +import android.os.Bundle; +import android.os.Environment; +import android.view.Menu; +import android.view.MenuItem; +import android.view.MotionEvent; +import android.view.View; +import android.widget.LinearLayout; +import android.widget.ProgressBar; +import android.widget.RelativeLayout; +import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.ActionBar; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.app.ActivityCompat; +import androidx.core.content.ContextCompat; + +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.github.pwittchen.swipe.library.rx2.SimpleSwipeListener; +import com.github.pwittchen.swipe.library.rx2.Swipe; + +import java.io.File; + +import javax.inject.Inject; + +import butterknife.BindView; +import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.ContentFontStyle; +import ml.docilealligator.infinityforreddit.FontStyle; +import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.SharedPreferencesUtils; +import ml.docilealligator.infinityforreddit.TitleFontStyle; +import pl.droidsonroids.gif.GifImageView; + +public class ViewGIFActivity extends AppCompatActivity { + + public static final String IMAGE_URL_KEY = "IUK"; + public static final String FILE_NAME_KEY = "FNK"; + private static final int PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 0; + @BindView(R.id.parent_relative_layout_view_gif_activity) + RelativeLayout mRelativeLayout; + @BindView(R.id.progress_bar_view_gif_activity) + ProgressBar mProgressBar; + @BindView(R.id.image_view_view_gif_activity) + GifImageView mImageView; + @BindView(R.id.load_image_error_linear_layout_view_gif_activity) + LinearLayout mLoadErrorLinearLayout; + @Inject + SharedPreferences mSharedPreferences; + private boolean isActionBarHidden = false; + private boolean isDownloading = false; + private Menu mMenu; + private Swipe swipe; + private String mImageUrl; + private String mImageFileName; + private float totalLengthY = 0.0f; + private float touchY = -1.0f; + private float zoom = 1.0f; + private boolean isSwiping = false; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + ((Infinity) getApplication()).getAppComponent().inject(this); + + getTheme().applyStyle(R.style.Theme_Default, true); + + getTheme().applyStyle(FontStyle.valueOf(mSharedPreferences + .getString(SharedPreferencesUtils.FONT_SIZE_KEY, FontStyle.Normal.name())).getResId(), true); + + getTheme().applyStyle(TitleFontStyle.valueOf(mSharedPreferences + .getString(SharedPreferencesUtils.TITLE_FONT_SIZE_KEY, TitleFontStyle.Normal.name())).getResId(), true); + + getTheme().applyStyle(ContentFontStyle.valueOf(mSharedPreferences + .getString(SharedPreferencesUtils.CONTENT_FONT_SIZE_KEY, ContentFontStyle.Normal.name())).getResId(), true); + + setContentView(R.layout.activity_view_gif); + + ButterKnife.bind(this); + + ActionBar actionBar = getSupportActionBar(); + Drawable upArrow = getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp); + actionBar.setHomeAsUpIndicator(upArrow); + actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.transparentActionBarAndExoPlayerControllerColor))); + setTitle(""); + + Intent intent = getIntent(); + mImageUrl = intent.getStringExtra(IMAGE_URL_KEY); + mImageFileName = intent.getStringExtra(FILE_NAME_KEY); + + mLoadErrorLinearLayout.setOnClickListener(view -> { + if (!isSwiping) { + mProgressBar.setVisibility(View.VISIBLE); + mLoadErrorLinearLayout.setVisibility(View.GONE); + loadImage(); + } + }); + + float pxHeight = getResources().getDisplayMetrics().heightPixels; + + int activityColorFrom = getResources().getColor(android.R.color.black); + int actionBarColorFrom = getResources().getColor(R.color.transparentActionBarAndExoPlayerControllerColor); + int actionBarElementColorFrom = getResources().getColor(android.R.color.white); + int colorTo = getResources().getColor(android.R.color.transparent); + + final ValueAnimator activityColorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), activityColorFrom, colorTo); + final ValueAnimator actionBarColorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), actionBarColorFrom, colorTo); + final ValueAnimator actionBarElementColorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), actionBarElementColorFrom, colorTo); + + activityColorAnimation.setDuration(300); // milliseconds + actionBarColorAnimation.setDuration(300); + actionBarElementColorAnimation.setDuration(300); + + activityColorAnimation.addUpdateListener(valueAnimator -> mRelativeLayout.setBackgroundColor((int) valueAnimator.getAnimatedValue())); + + actionBarColorAnimation.addUpdateListener(valueAnimator -> actionBar.setBackgroundDrawable(new ColorDrawable((int) valueAnimator.getAnimatedValue()))); + + actionBarElementColorAnimation.addUpdateListener(valueAnimator -> { + upArrow.setColorFilter((int) valueAnimator.getAnimatedValue(), PorterDuff.Mode.SRC_IN); + if (mMenu != null) { + Drawable drawable = mMenu.getItem(0).getIcon(); + //drawable.mutate(); + drawable.setColorFilter((int) valueAnimator.getAnimatedValue(), PorterDuff.Mode.SRC_IN); + } + }); + + loadImage(); + + swipe = new Swipe(); + swipe.setListener(new SimpleSwipeListener() { + @Override + public void onSwipingUp(final MotionEvent event) { + isSwiping = true; + float nowY = event.getY(); + float offset; + if (touchY == -1.0f) { + offset = 0.0f; + } else { + offset = nowY - touchY; + } + totalLengthY += offset; + touchY = nowY; + mImageView.animate() + .y(totalLengthY) + .setDuration(0) + .start(); + mLoadErrorLinearLayout.animate() + .y(totalLengthY) + .setDuration(0) + .start(); + } + + @Override + public boolean onSwipedUp(final MotionEvent event) { + if (totalLengthY < -pxHeight / 8) { + mImageView.animate() + .y(-pxHeight) + .setDuration(300) + .setListener(new Animator.AnimatorListener() { + @Override + public void onAnimationStart(Animator animator) { + activityColorAnimation.start(); + actionBarColorAnimation.start(); + actionBarElementColorAnimation.start(); + } + + @Override + public void onAnimationEnd(Animator animator) { + finish(); + } + + @Override + public void onAnimationCancel(Animator animator) { + } + + @Override + public void onAnimationRepeat(Animator animator) { + } + }) + .start(); + mLoadErrorLinearLayout.animate() + .y(-pxHeight) + .setDuration(300) + .start(); + } else { + isSwiping = false; + mImageView.animate() + .y(0) + .setDuration(300) + .start(); + mLoadErrorLinearLayout.animate() + .y(0) + .setDuration(300) + .start(); + } + + totalLengthY = 0.0f; + touchY = -1.0f; + return false; + } + + @Override + public void onSwipingDown(final MotionEvent event) { + isSwiping = true; + float nowY = event.getY(); + float offset; + if (touchY == -1.0f) { + offset = 0.0f; + } else { + offset = nowY - touchY; + } + totalLengthY += offset; + touchY = nowY; + mImageView.animate() + .y(totalLengthY) + .setDuration(0) + .start(); + mLoadErrorLinearLayout.animate() + .y(totalLengthY) + .setDuration(0) + .start(); + } + + @Override + public boolean onSwipedDown(final MotionEvent event) { + if (totalLengthY > pxHeight / 8) { + mImageView.animate() + .y(pxHeight) + .setDuration(300) + .setListener(new Animator.AnimatorListener() { + @Override + public void onAnimationStart(Animator animator) { + activityColorAnimation.start(); + actionBarColorAnimation.start(); + actionBarElementColorAnimation.start(); + } + + @Override + public void onAnimationEnd(Animator animator) { + finish(); + } + + @Override + public void onAnimationCancel(Animator animator) { + } + + @Override + public void onAnimationRepeat(Animator animator) { + } + }) + .start(); + mLoadErrorLinearLayout.animate() + .y(pxHeight) + .setDuration(300) + .start(); + } else { + isSwiping = false; + mImageView.animate() + .y(0) + .setDuration(300) + .start(); + mLoadErrorLinearLayout.animate() + .y(0) + .setDuration(300) + .start(); + } + + totalLengthY = 0.0f; + touchY = -1.0f; + + return false; + } + }); + + mImageView.setOnClickListener(view -> { + if (isActionBarHidden) { + getWindow().getDecorView().setSystemUiVisibility( + View.SYSTEM_UI_FLAG_LAYOUT_STABLE + | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION + | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); + isActionBarHidden = false; + } else { + getWindow().getDecorView().setSystemUiVisibility( + View.SYSTEM_UI_FLAG_LAYOUT_STABLE + | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION + | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN + | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION + | View.SYSTEM_UI_FLAG_FULLSCREEN + | View.SYSTEM_UI_FLAG_IMMERSIVE); + isActionBarHidden = true; + } + }); + } + + private void loadImage() { + Glide.with(this).load(mImageUrl).listener(new RequestListener<Drawable>() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { + mProgressBar.setVisibility(View.GONE); + mLoadErrorLinearLayout.setVisibility(View.VISIBLE); + return false; + } + + @Override + public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { + mProgressBar.setVisibility(View.GONE); + return false; + } + }).apply(new RequestOptions().fitCenter()).into(mImageView); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + mMenu = menu; + getMenuInflater().inflate(R.menu.view_gif_activity, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + finish(); + return true; + case R.id.action_download_view_gif: + if (isDownloading) { + return false; + } + + isDownloading = true; + + if (Build.VERSION.SDK_INT >= 23) { + if (ContextCompat.checkSelfPermission(this, + Manifest.permission.WRITE_EXTERNAL_STORAGE) + != PackageManager.PERMISSION_GRANTED) { + + // Permission is not granted + // No explanation needed; request the permission + ActivityCompat.requestPermissions(this, + new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, + PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE); + } else { + // Permission has already been granted + download(); + } + } else { + download(); + } + + return true; + } + + return false; + } + + @Override + public boolean dispatchTouchEvent(MotionEvent ev) { + if (zoom == 1.0) { + swipe.dispatchTouchEvent(ev); + } + return super.dispatchTouchEvent(ev); + } + + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + if (requestCode == PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE && grantResults.length > 0) { + if (grantResults[0] == PackageManager.PERMISSION_DENIED) { + Toast.makeText(this, R.string.no_storage_permission, Toast.LENGTH_SHORT).show(); + } else if (grantResults[0] == PackageManager.PERMISSION_GRANTED && isDownloading) { + download(); + } + isDownloading = false; + } + } + + private void download() { + DownloadManager.Request request = new DownloadManager.Request(Uri.parse(mImageUrl)); + request.setTitle(mImageFileName); + + request.allowScanningByMediaScanner(); + request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); + + //Android Q support + if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, mImageFileName); + } else { + String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString(); + File directory = new File(path + "/Infinity/"); + boolean saveToInfinityFolder = true; + if (!directory.exists()) { + if (!directory.mkdir()) { + saveToInfinityFolder = false; + } + } else { + if (directory.isFile()) { + if (!(directory.delete() && directory.mkdir())) { + saveToInfinityFolder = false; + } + } + } + + if (saveToInfinityFolder) { + request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES + "/Infinity/", mImageFileName); + } else { + request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, mImageFileName); + } + } + + DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); + + if (manager == null) { + Toast.makeText(this, R.string.download_failed, Toast.LENGTH_SHORT).show(); + return; + } + + manager.enqueue(request); + Toast.makeText(this, R.string.download_started, Toast.LENGTH_SHORT).show(); + } + + @Override + protected void onPause() { + super.onPause(); + overridePendingTransition(0, 0); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageActivity.java index 9d1d20af..652db78e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageActivity.java @@ -59,7 +59,6 @@ import ml.docilealligator.infinityforreddit.TitleFontStyle; public class ViewImageActivity extends AppCompatActivity { - public static final String TITLE_KEY = "TK"; public static final String IMAGE_URL_KEY = "IUK"; public static final String FILE_NAME_KEY = "FNK"; private static final int PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 0; @@ -113,7 +112,7 @@ public class ViewImageActivity extends AppCompatActivity { Intent intent = getIntent(); mImageUrl = intent.getStringExtra(IMAGE_URL_KEY); - mImageFileName = intent.getStringExtra(FILE_NAME_KEY) + ".jpg"; + mImageFileName = intent.getStringExtra(FILE_NAME_KEY); mLoadErrorLinearLayout.setOnClickListener(view -> { if (!isSwiping) { @@ -382,10 +381,10 @@ public class ViewImageActivity extends AppCompatActivity { PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE); } else { // Permission has already been granted - saveImage(); + download(); } } else { - saveImage(); + download(); } return true; @@ -408,13 +407,13 @@ public class ViewImageActivity extends AppCompatActivity { if (grantResults[0] == PackageManager.PERMISSION_DENIED) { Toast.makeText(this, R.string.no_storage_permission, Toast.LENGTH_SHORT).show(); } else if (grantResults[0] == PackageManager.PERMISSION_GRANTED && isDownloading) { - saveImage(); + download(); } isDownloading = false; } } - private void saveImage() { + private void download() { DownloadManager.Request request = new DownloadManager.Request(Uri.parse(mImageUrl)); request.setTitle(mImageFileName); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java index 2a64eab3..bf5bd92d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java @@ -203,12 +203,15 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp mMessageFullname = savedInstanceState.getString(MESSAGE_FULLNAME_STATE); mNewAccountName = savedInstanceState.getString(NEW_ACCOUNT_NAME_STATE); + mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE_KEY); + getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_view_subreddit_detail_activity, mFragment).commit(); + if (!mNullAccessToken && mAccessToken == null) { getCurrentAccountAndBindView(); } else { bindView(false); - mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE_KEY); - getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_view_subreddit_detail_activity, mFragment).commit(); + /*mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE_KEY); + getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_view_subreddit_detail_activity, mFragment).commit();*/ } if (mFetchSubredditInfoSuccess) { @@ -258,9 +261,8 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp glide.load(subredditData.getBannerUrl()).into(bannerImageView); bannerImageView.setOnClickListener(view -> { Intent intent = new Intent(ViewSubredditDetailActivity.this, ViewImageActivity.class); - intent.putExtra(ViewImageActivity.TITLE_KEY, title); intent.putExtra(ViewImageActivity.IMAGE_URL_KEY, subredditData.getBannerUrl()); - intent.putExtra(ViewImageActivity.FILE_NAME_KEY, subredditName + "-banner"); + intent.putExtra(ViewImageActivity.FILE_NAME_KEY, subredditName + "-banner.jpg"); startActivity(intent); }); } @@ -280,9 +282,8 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp .into(iconGifImageView); iconGifImageView.setOnClickListener(view -> { Intent intent = new Intent(ViewSubredditDetailActivity.this, ViewImageActivity.class); - intent.putExtra(ViewImageActivity.TITLE_KEY, title); intent.putExtra(ViewImageActivity.IMAGE_URL_KEY, subredditData.getIconUrl()); - intent.putExtra(ViewImageActivity.FILE_NAME_KEY, subredditName + "-icon"); + intent.putExtra(ViewImageActivity.FILE_NAME_KEY, subredditName + "-icon.jpg"); startActivity(intent); }); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java index fa86fb67..ec574dec 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java @@ -279,9 +279,8 @@ public class ViewUserDetailActivity extends BaseActivity implements UserThingSor glide.load(userData.getBanner()).into(bannerImageView); bannerImageView.setOnClickListener(view -> { Intent intent = new Intent(this, ViewImageActivity.class); - intent.putExtra(ViewImageActivity.TITLE_KEY, title); intent.putExtra(ViewImageActivity.IMAGE_URL_KEY, userData.getBanner()); - intent.putExtra(ViewImageActivity.FILE_NAME_KEY, username + "-banner"); + intent.putExtra(ViewImageActivity.FILE_NAME_KEY, username + "-banner.jpg"); startActivity(intent); }); } @@ -302,9 +301,8 @@ public class ViewUserDetailActivity extends BaseActivity implements UserThingSor iconGifImageView.setOnClickListener(view -> { Intent intent = new Intent(this, ViewImageActivity.class); - intent.putExtra(ViewImageActivity.TITLE_KEY, title); intent.putExtra(ViewImageActivity.IMAGE_URL_KEY, userData.getIconUrl()); - intent.putExtra(ViewImageActivity.FILE_NAME_KEY, username + "-icon"); + intent.putExtra(ViewImageActivity.FILE_NAME_KEY, username + "-icon.jpg"); startActivity(intent); }); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java index 222ecbf8..933b998f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java @@ -50,7 +50,6 @@ import ml.docilealligator.infinityforreddit.R; public class ViewVideoActivity extends AppCompatActivity { - public static final String TITLE_KEY = "TK"; public static final String SUBREDDIT_KEY = "SK"; public static final String ID_KEY = "IK"; private static final int PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 0; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java index f7e16eb1..e2e72171 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java @@ -53,6 +53,7 @@ import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import ml.docilealligator.infinityforreddit.Activity.CommentActivity; import ml.docilealligator.infinityforreddit.Activity.FilteredThingActivity; import ml.docilealligator.infinityforreddit.Activity.LinkResolverActivity; +import ml.docilealligator.infinityforreddit.Activity.ViewGIFActivity; import ml.docilealligator.infinityforreddit.Activity.ViewImageActivity; import ml.docilealligator.infinityforreddit.Activity.ViewPostDetailActivity; import ml.docilealligator.infinityforreddit.Activity.ViewSubredditDetailActivity; @@ -419,9 +420,8 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy ((PostDetailViewHolder) holder).mImageView.setOnClickListener(view -> { Intent intent = new Intent(mActivity, ViewImageActivity.class); intent.putExtra(ViewImageActivity.IMAGE_URL_KEY, mPost.getUrl()); - intent.putExtra(ViewImageActivity.TITLE_KEY, mPost.getTitle()); intent.putExtra(ViewImageActivity.FILE_NAME_KEY, mPost.getSubredditNamePrefixed().substring(2) - + "-" + mPost.getId().substring(3)); + + "-" + mPost.getId().substring(3) + ".jpg"); mActivity.startActivity(intent); }); break; @@ -443,16 +443,14 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy mActivity.startActivity(intent); }); break; - case Post.GIF_VIDEO_TYPE: + case Post.GIF_TYPE: ((PostDetailViewHolder) holder).mTypeTextView.setText("GIF"); - final Uri gifVideoUri = Uri.parse(mPost.getVideoUrl()); ((PostDetailViewHolder) holder).mImageView.setOnClickListener(view -> { - Intent intent = new Intent(mActivity, ViewVideoActivity.class); - intent.setData(gifVideoUri); - intent.putExtra(ViewVideoActivity.TITLE_KEY, mPost.getTitle()); - intent.putExtra(ViewVideoActivity.SUBREDDIT_KEY, mPost.getSubredditName()); - intent.putExtra(ViewVideoActivity.ID_KEY, mPost.getId()); + Intent intent = new Intent(mActivity, ViewGIFActivity.class); + intent.putExtra(ViewGIFActivity.FILE_NAME_KEY, mPost.getSubredditName() + + "-" + mPost.getId() + ".gif"); + intent.putExtra(ViewGIFActivity.IMAGE_URL_KEY, mPost.getVideoUrl()); mActivity.startActivity(intent); }); break; @@ -463,7 +461,6 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy ((PostDetailViewHolder) holder).mImageView.setOnClickListener(view -> { Intent intent = new Intent(mActivity, ViewVideoActivity.class); intent.setData(videoUri); - intent.putExtra(ViewVideoActivity.TITLE_KEY, mPost.getTitle()); intent.putExtra(ViewVideoActivity.SUBREDDIT_KEY, mPost.getSubredditName()); intent.putExtra(ViewVideoActivity.ID_KEY, mPost.getId()); mActivity.startActivity(intent); @@ -979,7 +976,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy } private int getParentPosition(int position) { - if (position < mVisibleComments.size()) { + if (position >= 0 && position < mVisibleComments.size()) { int childDepth = mVisibleComments.get(position).getDepth(); for (int i = position; i >= 0; i--) { if (mVisibleComments.get(i).getDepth() < childDepth) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java index 7f3f5f83..a0dbe900 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java @@ -45,6 +45,7 @@ import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import ml.docilealligator.infinityforreddit.Activity.CommentActivity; import ml.docilealligator.infinityforreddit.Activity.FilteredThingActivity; import ml.docilealligator.infinityforreddit.Activity.LinkResolverActivity; +import ml.docilealligator.infinityforreddit.Activity.ViewGIFActivity; import ml.docilealligator.infinityforreddit.Activity.ViewImageActivity; import ml.docilealligator.infinityforreddit.Activity.ViewPostDetailActivity; import ml.docilealligator.infinityforreddit.Activity.ViewSubredditDetailActivity; @@ -146,7 +147,8 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView if (holder instanceof DataViewHolder) { Post post = getItem(position); if (post != null) { - final String id = post.getFullName(); + final String fullName = post.getFullName(); + final String id = post.getId(); final String subredditNamePrefixed = post.getSubredditNamePrefixed(); String subredditName = subredditNamePrefixed.substring(2); String authorPrefixed = "u/" + post.getAuthor(); @@ -399,9 +401,8 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView ((DataViewHolder) holder).imageView.setOnClickListener(view -> { Intent intent = new Intent(mContext, ViewImageActivity.class); intent.putExtra(ViewImageActivity.IMAGE_URL_KEY, imageUrl); - intent.putExtra(ViewImageActivity.TITLE_KEY, title); - intent.putExtra(ViewImageActivity.FILE_NAME_KEY, subredditNamePrefixed.substring(2) - + "-" + id.substring(3)); + intent.putExtra(ViewImageActivity.FILE_NAME_KEY, subredditName + + "-" + id + ".jpg"); mContext.startActivity(intent); }); break; @@ -423,16 +424,16 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView mContext.startActivity(intent); }); break; - case Post.GIF_VIDEO_TYPE: + case Post.GIF_TYPE: ((DataViewHolder) holder).typeTextView.setText(R.string.gif); final Uri gifVideoUri = Uri.parse(post.getVideoUrl()); ((DataViewHolder) holder).imageView.setOnClickListener(view -> { - Intent intent = new Intent(mContext, ViewVideoActivity.class); + Intent intent = new Intent(mContext, ViewGIFActivity.class); intent.setData(gifVideoUri); - intent.putExtra(ViewVideoActivity.TITLE_KEY, title); - intent.putExtra(ViewVideoActivity.SUBREDDIT_KEY, subredditName); - intent.putExtra(ViewVideoActivity.ID_KEY, id); + intent.putExtra(ViewGIFActivity.FILE_NAME_KEY, subredditName + + "-" + id + ".gif"); + intent.putExtra(ViewGIFActivity.IMAGE_URL_KEY, post.getVideoUrl()); mContext.startActivity(intent); }); break; @@ -443,9 +444,8 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView ((DataViewHolder) holder).imageView.setOnClickListener(view -> { Intent intent = new Intent(mContext, ViewVideoActivity.class); intent.setData(videoUri); - intent.putExtra(ViewVideoActivity.TITLE_KEY, title); intent.putExtra(ViewVideoActivity.SUBREDDIT_KEY, subredditName); - intent.putExtra(ViewVideoActivity.ID_KEY, id); + intent.putExtra(ViewVideoActivity.ID_KEY, fullName); mContext.startActivity(intent); }); break; @@ -541,7 +541,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView EventBus.getDefault().post(new PostUpdateEventToDetailActivity(post)); } - }, id, newVoteType, holder.getAdapterPosition()); + }, fullName, newVoteType, holder.getAdapterPosition()); }); ((DataViewHolder) holder).downvoteButton.setOnClickListener(view -> { @@ -612,7 +612,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView EventBus.getDefault().post(new PostUpdateEventToDetailActivity(post)); } - }, id, newVoteType, holder.getAdapterPosition()); + }, fullName, newVoteType, holder.getAdapterPosition()); }); ((DataViewHolder) holder).commentButton.setOnClickListener(view -> { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index f82237a4..4c10e9c2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -23,6 +23,7 @@ import ml.docilealligator.infinityforreddit.Activity.SearchSubredditsResultActiv import ml.docilealligator.infinityforreddit.Activity.SettingsActivity; import ml.docilealligator.infinityforreddit.Activity.SubredditSelectionActivity; import ml.docilealligator.infinityforreddit.Activity.SubscribedThingListingActivity; +import ml.docilealligator.infinityforreddit.Activity.ViewGIFActivity; import ml.docilealligator.infinityforreddit.Activity.ViewImageActivity; import ml.docilealligator.infinityforreddit.Activity.ViewMessageActivity; import ml.docilealligator.infinityforreddit.Activity.ViewPostDetailActivity; @@ -112,4 +113,6 @@ public interface AppComponent { void inject(AccountSavedThingActivity accountSavedThingActivity); void inject(ViewImageActivity viewImageActivity); + + void inject(ViewGIFActivity viewGIFActivity); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java index a12c1d33..9ab9b097 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java @@ -151,7 +151,7 @@ public class ParsePost { } else if (data.has(JSONUtils.PREVIEW_KEY)) { if (data.getJSONObject(JSONUtils.PREVIEW_KEY).has(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY)) { //Gif video post (HLS) - int postType = Post.GIF_VIDEO_TYPE; + int postType = Post.VIDEO_TYPE; String videoUrl = Html.fromHtml(data.getJSONObject(JSONUtils.PREVIEW_KEY) .getJSONObject(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY).getString(JSONUtils.HLS_URL_KEY)).toString(); @@ -174,6 +174,17 @@ public class ParsePost { post.setPreviewWidth(previewWidth); post.setPreviewHeight(previewHeight); + } else if (url.endsWith("gif")){ + //Gif post + int postType = Post.GIF_TYPE; + post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, + formattedPostTime, title, previewUrl, url, permalink, score, postType, + voteType, gilded, nComments, flair, hidden, spoiler, nsfw, stickied, + archived, locked, saved, isCrosspost); + + post.setPreviewWidth(previewWidth); + post.setPreviewHeight(previewHeight); + post.setVideoUrl(url); } else { if (url.contains(permalink)) { //Text post but with a preview diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseUserData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseUserData.java index b3cb14f7..e0fa6651 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseUserData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseUserData.java @@ -20,6 +20,10 @@ public class ParseUserData { } private static UserData parseUserDataBase(JSONObject userDataJson) throws JSONException { + if(userDataJson == null) { + return null; + } + userDataJson = userDataJson.getJSONObject(JSONUtils.DATA_KEY); String userName = userDataJson.getString(JSONUtils.NAME_KEY); String iconImageUrl = userDataJson.getString(JSONUtils.ICON_IMG_KEY); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java index 8e2e40de..2aee9455 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java @@ -13,7 +13,7 @@ public class Post implements Parcelable { public static final int IMAGE_TYPE = 1; public static final int LINK_TYPE = 2; public static final int VIDEO_TYPE = 3; - public static final int GIF_VIDEO_TYPE = 4; + public static final int GIF_TYPE = 4; public static final int NO_PREVIEW_LINK_TYPE = 5; public static final Creator<Post> CREATOR = new Creator<Post>() { @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java index 429ed567..83c7a072 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java @@ -164,7 +164,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { this.params = params; this.callback = callback; - if (params.key.equals("") || params.key.equals("null")) { + if ("".equals(params.key) || "null".equals(params.key)) { return; } diff --git a/app/src/main/res/layout/activity_view_gif.xml b/app/src/main/res/layout/activity_view_gif.xml new file mode 100644 index 00000000..b1821e41 --- /dev/null +++ b/app/src/main/res/layout/activity_view_gif.xml @@ -0,0 +1,41 @@ +<?xml version="1.0" encoding="utf-8"?> +<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto" + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:id="@+id/parent_relative_layout_view_gif_activity" + android:background="@android:color/black" + tools:application="ml.docilealligator.infinityforreddit.Activity.ViewGIFActivity"> + + <ProgressBar + android:id="@+id/progress_bar_view_gif_activity" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_centerInParent="true" /> + + <pl.droidsonroids.gif.GifImageView + android:id="@+id/image_view_view_gif_activity" + android:layout_width="match_parent" + android:layout_height="match_parent" + app:gest_fillViewport="true" /> + + <LinearLayout + android:id="@+id/load_image_error_linear_layout_view_gif_activity" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:visibility="gone"> + + <TextView + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:drawableTop="@drawable/ic_error_outline_white_24dp" + android:layout_gravity="center" + android:gravity="center" + android:textColor="@android:color/white" + android:text="@string/tap_to_retry" + android:textSize="?attr/font_default" /> + + </LinearLayout> + +</RelativeLayout> diff --git a/app/src/main/res/menu/view_gif_activity.xml b/app/src/main/res/menu/view_gif_activity.xml new file mode 100644 index 00000000..493c9fdb --- /dev/null +++ b/app/src/main/res/menu/view_gif_activity.xml @@ -0,0 +1,10 @@ +<?xml version="1.0" encoding="utf-8"?> +<menu xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto"> + <item + android:id="@+id/action_download_view_gif" + android:orderInCategory="1" + android:title="@string/action_download" + android:icon="@drawable/ic_file_download_white_24dp" + app:showAsAction="always" /> +</menu>
\ No newline at end of file |