From c28fddc4dd0bb19ea8317fa16cbfd095127c7a7e Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Sun, 19 Feb 2017 16:07:45 +0100 Subject: [PATCH] did some finetuning --- .../org/schabi/newpipe/ChannelActivity.java | 22 +++++- .../detail/VideoItemDetailActivity.java | 9 ++- .../newpipe/download/DownloadActivity.java | 12 +--- .../org/schabi/newpipe/util/NavStack.java | 70 +++++++++++++------ app/src/main/res/menu/download_menu.xml | 5 -- 5 files changed, 74 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/ChannelActivity.java b/app/src/main/java/org/schabi/newpipe/ChannelActivity.java index 4c7df1e5f..c8e0ad4a8 100644 --- a/app/src/main/java/org/schabi/newpipe/ChannelActivity.java +++ b/app/src/main/java/org/schabi/newpipe/ChannelActivity.java @@ -91,9 +91,16 @@ public class ChannelActivity extends AppCompatActivity { Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); rootView = findViewById(R.id.rootView); setSupportActionBar(toolbar); - Intent i = getIntent(); - channelUrl = i.getStringExtra(NavStack.URL); - serviceId = i.getIntExtra(NavStack.SERVICE_ID, -1); + if(savedInstanceState == null) { + Intent i = getIntent(); + channelUrl = i.getStringExtra(NavStack.URL); + serviceId = i.getIntExtra(NavStack.SERVICE_ID, -1); + } else { + channelUrl = savedInstanceState.getString(NavStack.URL); + serviceId = savedInstanceState.getInt(NavStack.SERVICE_ID); + NavStack.getInstance() + .restoreSavedInstanceState(savedInstanceState); + } infoListAdapter = new InfoListAdapter(this, rootView); @@ -136,6 +143,15 @@ public class ChannelActivity extends AppCompatActivity { requestData(false); } + @Override + public void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + outState.putString(NavStack.URL, channelUrl); + outState.putInt(NavStack.SERVICE_ID, serviceId); + NavStack.getInstance() + .onSaveInstanceState(outState); + } + private void updateUi(final ChannelInfo info) { CollapsingToolbarLayout ctl = (CollapsingToolbarLayout) findViewById(R.id.channel_toolbar_layout); ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar); diff --git a/app/src/main/java/org/schabi/newpipe/detail/VideoItemDetailActivity.java b/app/src/main/java/org/schabi/newpipe/detail/VideoItemDetailActivity.java index 9cb5e055d..261e219cf 100644 --- a/app/src/main/java/org/schabi/newpipe/detail/VideoItemDetailActivity.java +++ b/app/src/main/java/org/schabi/newpipe/detail/VideoItemDetailActivity.java @@ -88,6 +88,8 @@ public class VideoItemDetailActivity extends ThemableActivity { } else { videoUrl = savedInstanceState.getString(NavStack.URL); currentStreamingService = savedInstanceState.getInt(NavStack.SERVICE_ID); + NavStack.getInstance() + .restoreSavedInstanceState(savedInstanceState); addFragment(savedInstanceState); } } @@ -150,6 +152,8 @@ public class VideoItemDetailActivity extends ThemableActivity { outState.putString(NavStack.URL, videoUrl); outState.putInt(NavStack.SERVICE_ID, currentStreamingService); outState.putBoolean(VideoItemDetailFragment.AUTO_PLAY, false); + NavStack.getInstance() + .onSaveInstanceState(outState); } @Override @@ -164,9 +168,8 @@ public class VideoItemDetailActivity extends ThemableActivity { // http://developer.android.com/design/patterns/navigation.html#up-vs-back - Intent intent = new Intent(this, MainActivity.class); - intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); - NavUtils.navigateUpTo(this, intent); + NavStack.getInstance() + .openMainActivity(this); return true; } else { return super.onOptionsItemSelected(item); diff --git a/app/src/main/java/org/schabi/newpipe/download/DownloadActivity.java b/app/src/main/java/org/schabi/newpipe/download/DownloadActivity.java index 2cf8cedc9..3c80e3be0 100644 --- a/app/src/main/java/org/schabi/newpipe/download/DownloadActivity.java +++ b/app/src/main/java/org/schabi/newpipe/download/DownloadActivity.java @@ -242,9 +242,7 @@ public class DownloadActivity extends ThemableActivity implements AdapterView.On switch (id) { case android.R.id.home: { - Intent intent = new Intent(this, org.schabi.newpipe.MainActivity.class); - intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); - NavUtils.navigateUpTo(this, intent); + onBackPressed(); return true; } case R.id.action_settings: { @@ -252,14 +250,6 @@ public class DownloadActivity extends ThemableActivity implements AdapterView.On startActivity(intent); return true; } - case R.id.action_report_error: { - ErrorActivity.reportError(DownloadActivity.this, new Vector(), - null, null, - ErrorActivity.ErrorInfo.make(ErrorActivity.USER_REPORT, - null, - "user_report", R.string.user_report)); - return true; - } default: return super.onOptionsItemSelected(item); } diff --git a/app/src/main/java/org/schabi/newpipe/util/NavStack.java b/app/src/main/java/org/schabi/newpipe/util/NavStack.java index b9f0165bc..f84c5725e 100644 --- a/app/src/main/java/org/schabi/newpipe/util/NavStack.java +++ b/app/src/main/java/org/schabi/newpipe/util/NavStack.java @@ -3,6 +3,7 @@ package org.schabi.newpipe.util; import android.app.Activity; import android.content.Context; import android.content.Intent; +import android.os.Bundle; import android.support.v4.app.NavUtils; import android.support.v7.app.AppCompatActivity; import android.util.Log; @@ -14,6 +15,7 @@ import org.schabi.newpipe.detail.VideoItemDetailFragment; import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.StreamingService; +import java.util.ArrayList; import java.util.Stack; /** @@ -45,6 +47,8 @@ public class NavStack { public static final String SERVICE_ID = "service_id"; public static final String URL = "url"; + private static final String NAV_STACK="nav_stack"; + private enum ActivityId { CHANNEL, DETAIL @@ -74,33 +78,39 @@ public class NavStack { } public void navBack(Activity activity) throws Exception { + if(stack.size() == 0) { // if stack is already empty here, activity was probably called + // from another app + activity.finish(); + return; + } stack.pop(); // remove curent activty, since we dont want to return to itself - if(stack.size() == 0) { + if (stack.size() == 0) { openMainActivity(activity); // if no more page is on the stack this means we are home - } else { - NavEntry entry = stack.pop(); // this element will reapear, since by calling the old page - // this element will be pushed on top again - try { - StreamingService service = NewPipe.getService(entry.serviceId); - switch (service.getLinkTypeByUrl(entry.url)) { - case STREAM: - openDetailActivity(activity, entry.url, entry.serviceId); - break; - case CHANNEL: - openChannelActivity(activity, entry.url, entry.serviceId); - break; - case NONE: - throw new Exception("Url not known to service. service=" - + Integer.toString(entry.serviceId) + " url=" + entry.url); - default: - openMainActivity(activity); - } - } catch (Exception e) { - e.printStackTrace(); + return; + } + NavEntry entry = stack.pop(); // this element will reapear, since by calling the old page + // this element will be pushed on top again + try { + StreamingService service = NewPipe.getService(entry.serviceId); + switch (service.getLinkTypeByUrl(entry.url)) { + case STREAM: + openDetailActivity(activity, entry.url, entry.serviceId); + break; + case CHANNEL: + openChannelActivity(activity, entry.url, entry.serviceId); + break; + case NONE: + throw new Exception("Url not known to service. service=" + + Integer.toString(entry.serviceId) + " url=" + entry.url); + default: + openMainActivity(activity); } + } catch (Exception e) { + e.printStackTrace(); } } + public void openChannelActivity(Context context, String url, int serviceId) { openActivity(context, url, serviceId, ChannelActivity.class); } @@ -117,9 +127,25 @@ public class NavStack { context.startActivity(i); } - private void openMainActivity(Activity a) { + public void openMainActivity(Activity a) { + stack.clear(); Intent i = new Intent(a, MainActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); NavUtils.navigateUpTo(a, i); } + + public void onSaveInstanceState(Bundle state) { + ArrayList sa = new ArrayList<>(); + for(NavEntry entry : stack) { + sa.add(entry.url); + } + state.putStringArrayList(NAV_STACK, sa); + } + + public void restoreSavedInstanceState(Bundle state) { + ArrayList sa = state.getStringArrayList(NAV_STACK); + for(String url : sa) { + stack.push(new NavEntry(url, NewPipe.getServiceByUrl(url).getServiceId())); + } + } } diff --git a/app/src/main/res/menu/download_menu.xml b/app/src/main/res/menu/download_menu.xml index a396791d0..4cbb845ea 100644 --- a/app/src/main/res/menu/download_menu.xml +++ b/app/src/main/res/menu/download_menu.xml @@ -2,12 +2,7 @@ - - - \ No newline at end of file