From 64c423902ae33efb0068bb3b99165203f1b0739b Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Thu, 25 Feb 2016 03:43:13 +0100 Subject: [PATCH] made error handling work a bit --- app/build.gradle | 1 + app/src/main/AndroidManifest.xml | 1 + .../schabi/newpipe/ActivityCommunicator.java | 8 +- .../newpipe/VideoItemDetailFragment.java | 21 +++- .../schabi/newpipe/VideoItemListActivity.java | 6 - .../newpipe/errorhandling/ErrorActivity.java | 110 ++++++++++++++++++ app/src/main/res/layout/activity_error.xml | 31 +---- app/src/main/res/values/strings.xml | 3 +- 8 files changed, 142 insertions(+), 39 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index c1f1fb00f..9ee5f1686 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -42,4 +42,5 @@ dependencies { compile 'de.hdodenhof:circleimageview:2.0.0' compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' compile 'com.github.nirhart:parallaxscroll:1.0' + compile 'org.apache.directory.studio:org.apache.commons.lang:2.6' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 618290f94..16a8c4999 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -106,6 +106,7 @@ diff --git a/app/src/main/java/org/schabi/newpipe/ActivityCommunicator.java b/app/src/main/java/org/schabi/newpipe/ActivityCommunicator.java index b09652a6c..92f9d35c8 100644 --- a/app/src/main/java/org/schabi/newpipe/ActivityCommunicator.java +++ b/app/src/main/java/org/schabi/newpipe/ActivityCommunicator.java @@ -22,10 +22,12 @@ package org.schabi.newpipe; import android.graphics.Bitmap; +import java.util.List; + /** * Singleton: * Used to send data between certain Activity/Services within the same process. - * This can be considered as hack inside the Android universe. **/ + * This can be considered as an ugly hack inside the Android universe. **/ public class ActivityCommunicator { private static ActivityCommunicator activityCommunicator = null; @@ -39,4 +41,8 @@ public class ActivityCommunicator { // Thumbnail send from VideoItemDetailFragment to BackgroundPlayer public volatile Bitmap backgroundPlayerThumbnail; + + // Sent from any activity to ErrorActivity. + public volatile List errorList; + public volatile Class returnActivity; } diff --git a/app/src/main/java/org/schabi/newpipe/VideoItemDetailFragment.java b/app/src/main/java/org/schabi/newpipe/VideoItemDetailFragment.java index 625f1d1dc..5999947d5 100644 --- a/app/src/main/java/org/schabi/newpipe/VideoItemDetailFragment.java +++ b/app/src/main/java/org/schabi/newpipe/VideoItemDetailFragment.java @@ -46,6 +46,7 @@ import com.nostra13.universalimageloader.core.listener.ImageLoadingListener; import java.util.ArrayList; import java.util.Vector; +import org.schabi.newpipe.errorhandling.ErrorActivity; import org.schabi.newpipe.extractor.MediaFormat; import org.schabi.newpipe.extractor.ParsingException; import org.schabi.newpipe.extractor.ServiceList; @@ -166,10 +167,23 @@ public class VideoItemDetailFragment extends Fragment { }); e.printStackTrace(); } catch (ParsingException e) { - postNewErrorToast(h, e.getMessage()); + ErrorActivity.reportError(h, getActivity(), e, 0, VideoItemListActivity.class); + h.post(new Runnable() { + @Override + public void run() { + getActivity().finish(); + } + }); e.printStackTrace(); } catch(Exception e) { - postNewErrorToast(h, R.string.general_error); + ErrorActivity.reportError(h, getActivity(), e, + R.string.general_error, VideoItemListActivity.class); + h.post(new Runnable() { + @Override + public void run() { + getActivity().finish(); + } + }); e.printStackTrace(); } finally { if(videoInfo != null && @@ -178,6 +192,9 @@ public class VideoItemDetailFragment extends Fragment { for(Exception e : videoInfo.errors) { e.printStackTrace(); } + //todo: do not call directly ask the user if it should be reported + ErrorActivity.reportError(h, getActivity(), + videoInfo.errors, 0, VideoItemDetailActivity.class); } } } diff --git a/app/src/main/java/org/schabi/newpipe/VideoItemListActivity.java b/app/src/main/java/org/schabi/newpipe/VideoItemListActivity.java index 71284c99f..1db977fd0 100644 --- a/app/src/main/java/org/schabi/newpipe/VideoItemListActivity.java +++ b/app/src/main/java/org/schabi/newpipe/VideoItemListActivity.java @@ -241,12 +241,6 @@ public class VideoItemListActivity extends AppCompatActivity } PreferenceManager.setDefaultValues(this, R.xml.settings, false); - - - /* this is for debuging only if this is still pressent in master branch kill the programmer */ - startActivity(new Intent(this, ErrorActivity.class)); - - //------------------------------------------------------------------------- } @Override diff --git a/app/src/main/java/org/schabi/newpipe/errorhandling/ErrorActivity.java b/app/src/main/java/org/schabi/newpipe/errorhandling/ErrorActivity.java index 6529fa188..25a17e6e7 100644 --- a/app/src/main/java/org/schabi/newpipe/errorhandling/ErrorActivity.java +++ b/app/src/main/java/org/schabi/newpipe/errorhandling/ErrorActivity.java @@ -1,15 +1,125 @@ package org.schabi.newpipe.errorhandling; + +import android.app.Activity; +import android.content.Context; +import android.content.Intent; +import android.support.v4.app.NavUtils; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.os.Handler; +import android.view.MenuItem; +import android.widget.TextView; +import org.apache.commons.lang.exception.ExceptionUtils; +import org.schabi.newpipe.ActivityCommunicator; import org.schabi.newpipe.R; +import org.schabi.newpipe.VideoItemListActivity; + +import java.util.List; +import java.util.Vector; public class ErrorActivity extends AppCompatActivity { + private List errorList; + private Class returnActivity; + + // views + private TextView errorView; + + public static void reportError(Context context, List el, int message, Class returnAcitivty) { + ActivityCommunicator ac = ActivityCommunicator.getCommunicator(); + ac.errorList = el; + ac.returnActivity = returnAcitivty; + Intent intent = new Intent(context, ErrorActivity.class); + context.startActivity(intent); + } + + public static void reportError(Context context, Exception e, int message, Class returnAcitivty) { + List el = new Vector<>(); + el.add(e); + reportError(context, el, message, returnAcitivty); + } + + // async call + public static void reportError(Handler handler, final Context context, + final Exception e, final int message, final Class returnAcitivty) { + List el = new Vector<>(); + el.add(e); + reportError(handler, context, el, message, returnAcitivty); + } + + // async call + public static void reportError(Handler handler, final Context context, + final List el, final int message, final Class returnAcitivty) { + handler.post(new Runnable() { + @Override + public void run() { + reportError(context, el, message, returnAcitivty); + } + }); + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_error); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + + ActivityCommunicator ac = ActivityCommunicator.getCommunicator(); + errorList = ac.errorList; + returnActivity = ac.returnActivity; + + errorView = (TextView) findViewById(R.id.errorView); + errorView.setText(formErrorText(errorList)); + + //importand add gurumeditaion + addGuruMeditaion(); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + int id = item.getItemId(); + if (id == android.R.id.home) { + goToReturnActivity(); + return true; + } + return false; + } + + private String formErrorText(List el) { + String text = ""; + for(Exception e : el) { + text += "-------------------------------------\n" + + ExceptionUtils.getStackTrace(e); + } + text += "-------------------------------------"; + return text; + } + + private void goToReturnActivity() { + Intent intent; + if(returnActivity != null && + returnActivity.isAssignableFrom(Activity.class)) { + intent = new Intent(this, returnActivity); + } else { + intent = new Intent(this, VideoItemListActivity.class); + } + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + NavUtils.navigateUpTo(this, intent); + } + + private void addGuruMeditaion() { + //just an easter egg + TextView sorryView = (TextView) findViewById(R.id.errorSorryView); + String text = sorryView.getText().toString(); + text += "\n" + getString(R.string.guru_meditation); + sorryView.setText(text); + } + + @Override + public void onBackPressed() { + super.onBackPressed(); + goToReturnActivity(); } } diff --git a/app/src/main/res/layout/activity_error.xml b/app/src/main/res/layout/activity_error.xml index 378c66bdb..8a9ae8e7b 100644 --- a/app/src/main/res/layout/activity_error.xml +++ b/app/src/main/res/layout/activity_error.xml @@ -24,6 +24,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" + android:gravity="center" android:text="@string/sorry_string" android:textStyle="bold" /> @@ -44,35 +45,7 @@ android:id="@+id/errorView" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:typeface="monospace" - android:text="asdkfjasdhfjshgfgnigregirenigneigheuigeorigjweogijhegiohegojiegoergjeogheoghweoghiehgohewgoiwehogihewrghieogh - asdfgeogirejgoeirjgoregjogijegoiwejg - wergjergljegoiejgoeigjogjeogjegohgeoigheognmgnbnorneog - ergoegremgekgnonhotnhotrnhkhnh - safs - f - s - f - f - f - safasf - sageoigohntrh - trhoijrhoirnhohintrionbobirnionb - boitrnobinobnroni - sadfsfagfkjgnsfdljhenvgoenvjibnoenpeoignweguihregpiorhnpoithtvhiohöio höio ho fvgegh gsfdsfd - sfdaiohsfdiousfdahoiuhsfdaoiusfd oifdhoifdho fvigh fvuigupi refsfdasklsdfhsalkjfhsalkfjsa - salgkjsfdgkjreog ehpio vpo hfgoeiwrhpwreo ibbuhwfgeubuh l jhjkb - - ssfda - sfda - sfda - sfda - - sagrejgorigjeoigjgkofdjgölkfdjgsdökgjklgjoe - sfdafgs - dfg - sd - fg"/> + android:typeface="monospace"/>