Java Code Examples for com.hippo.util.ExceptionUtils#throwIfFatal()

The following examples show how to use com.hippo.util.ExceptionUtils#throwIfFatal() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommonOperations.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
@Override
protected JSONObject doInBackground(Void... params) {
    String url;
    if (Settings.getBetaUpdateChannel()) {
        url = "https://raw.githubusercontent.com/axlecho/MHViewer/api/update_beta.json";
    } else {
        url = "https://raw.githubusercontent.com/axlecho/MHViewer/api/update.json";
    }

    try {
        return fetchUpdateInfo(url);
    } catch (Throwable e2) {
        ExceptionUtils.throwIfFatal(e2);
        return null;
    }
}
 
Example 2
Source File: EhTagDatabase.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
private static boolean save(OkHttpClient client, String url, File file) {
  Request request = new Request.Builder().url(url).build();
  Call call = client.newCall(request);
  try (Response response = call.execute()) {
    if (!response.isSuccessful()) {
      return false;
    }
    ResponseBody body = response.body();
    if (body == null) {
      return false;
    }

    try (InputStream is = body.byteStream(); OutputStream os = new FileOutputStream(file)) {
      IOUtils.copy(is, os);
    }

    return true;
  } catch (Throwable t) {
    ExceptionUtils.throwIfFatal(t);
    return false;
  }
}
 
Example 3
Source File: RestoreDownloadPreference.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
@Override
protected Object doInBackground(Void... params) {
    UniFile dir = Settings.getDownloadLocation();
    if (null == dir) {
        return null;
    }

    List<RestoreItem> restoreItemList = new ArrayList<>();

    UniFile[] files = dir.listFiles();
    if (files == null) {
        return null;
    }

    for (UniFile file: files) {
        RestoreItem restoreItem = getRestoreItem(file);
        if (null != restoreItem) {
            restoreItemList.add(restoreItem);
        }
    }

    if (0 == restoreItemList.size()) {
        return Collections.EMPTY_LIST;
    }

    try {
        return EhEngine.fillGalleryListByApi(null, mHttpClient, new ArrayList<GalleryInfo>(restoreItemList), EhUrl.getReferer());
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        e.printStackTrace();
        return null;
    }
}
 
Example 4
Source File: EhEngine.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
public static VoteCommentParser.Result voteComment(@Nullable EhClient.Task task, OkHttpClient okHttpClient,
        long apiUid, String apiKey, long gid, String token, long commentId, int commentVote) throws Throwable {
    final JSONObject json = new JSONObject();
    json.put("method", "votecomment");
    json.put("apiuid", apiUid);
    json.put("apikey", apiKey);
    json.put("gid", gid);
    json.put("token", token);
    json.put("comment_id", commentId);
    json.put("comment_vote", commentVote);
    final RequestBody requestBody = RequestBody.create(MEDIA_TYPE_JSON, json.toString());
    String url = EhUrl.getApiUrl();
    String referer = EhUrl.getReferer();
    String origin = EhUrl.getOrigin();
    Log.d(TAG, url);
    Request request = new EhRequestBuilder(url, referer, origin)
            .post(requestBody)
            .build();
    Call call = okHttpClient.newCall(request);

    // Put call
    if (null != task) {
        task.setCall(call);
    }

    String body = null;
    Headers headers = null;
    int code = -1;
    try {
        Response response = call.execute();
        code = response.code();
        headers = response.headers();
        body = response.body().string();
        return VoteCommentParser.parse(body, commentVote);
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        throwException(call, code, headers, body, e);
        throw e;
    }
}
 
Example 5
Source File: GalleryActivity.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
private void saveImageTo(int page) {
    if (null == mGalleryProvider) {
        return;
    }
    File dir = getCacheDir();
    UniFile file;
    if (null == (file = mGalleryProvider.save(page, UniFile.fromFile(dir), mGalleryProvider.getImageFilename(page)))) {
        Toast.makeText(this, R.string.error_cant_save_image, Toast.LENGTH_SHORT).show();
        return;
    }
    String filename = file.getName();
    if (filename == null) {
        Toast.makeText(this, R.string.error_cant_save_image, Toast.LENGTH_SHORT).show();
        return;
    }
    mCacheFileName = filename;
    Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("image/*");
    intent.putExtra(Intent.EXTRA_TITLE, filename);
    try {
        startActivityForResult(intent, WRITE_REQUEST_CODE);
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        Toast.makeText(this, R.string.error_cant_find_activity, Toast.LENGTH_SHORT).show();
    }
}
 
Example 6
Source File: EhEngine.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
public static ProfileParser.Result getProfile(@Nullable EhClient.Task task,
                                              OkHttpClient okHttpClient) throws Throwable {
    String url = EhUrl.URL_FORUMS;
    Log.d(TAG, url);
    Request request = new EhRequestBuilder(url, null).build();
    Call call = okHttpClient.newCall(request);

    // Put call
    if (null != task) {
        task.setCall(call);
    }

    String body = null;
    Headers headers = null;
    int code = -1;
    try {
        Response response = call.execute();
        code = response.code();
        headers = response.headers();
        body = response.body().string();
        return getProfileInternal(task, okHttpClient, ForumsParser.parse(body), url);
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        throwException(call, code, headers, body, e);
        throw e;
    }
}
 
Example 7
Source File: HackyTextInputEditText.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
@Override
public CharSequence getHint() {
  if (HAS_METHOD_UPDATE_CURSOR_POSITION_MZ && FIELD_M_HINT != null) {
    try {
      return getSuperHint();
    } catch (Throwable e) {
      ExceptionUtils.throwIfFatal(e);
      return super.getHint();
    }
  } else {
    return super.getHint();
  }
}
 
Example 8
Source File: EhEngine.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
public static String getGalleryToken(@Nullable EhClient.Task task, OkHttpClient okHttpClient,
        long gid, String gtoken, int page) throws Throwable {
    JSONObject json = new JSONObject()
            .put("method", "gtoken")
            .put("pagelist", new JSONArray().put(
                    new JSONArray().put(gid).put(gtoken).put(page + 1)));
    final RequestBody requestBody = RequestBody.create(MEDIA_TYPE_JSON, json.toString());
    String url = EhUrl.getApiUrl();
    String referer = EhUrl.getReferer();
    String origin = EhUrl.getOrigin();
    Log.d(TAG, url);
    Request request = new EhRequestBuilder(url, referer, origin)
            .post(requestBody)
            .build();
    Call call = okHttpClient.newCall(request);

    // Put call
    if (null != task) {
        task.setCall(call);
    }

    String body = null;
    Headers headers = null;
    int code = -1;
    try {
        Response response = call.execute();
        code = response.code();
        headers = response.headers();
        body = response.body().string();
        return GalleryTokenApiParser.parse(body);
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        throwException(call, code, headers, body, e);
        throw e;
    }
}
 
Example 9
Source File: EhEngine.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
public static String getGalleryToken(@Nullable EhClient.Task task, OkHttpClient okHttpClient,
                                     long gid, String gtoken, int page) throws Throwable {
    JSONObject json = new JSONObject()
            .put("method", "gtoken")
            .put("pagelist", new JSONArray().put(
                    new JSONArray().put(gid).put(gtoken).put(page + 1)));
    final RequestBody requestBody = RequestBody.create(MEDIA_TYPE_JSON, json.toString());
    String url = EhUrl.getApiUrl();
    String referer = EhUrl.getReferer();
    String origin = EhUrl.getOrigin();
    Log.d(TAG, url);
    Request request = new EhRequestBuilder(url, referer, origin)
            .post(requestBody)
            .build();
    Call call = okHttpClient.newCall(request);

    // Put call
    if (null != task) {
        task.setCall(call);
    }

    String body = null;
    Headers headers = null;
    int code = -1;
    try {
        Response response = call.execute();
        code = response.code();
        headers = response.headers();
        body = response.body().string();
        return GalleryTokenApiParser.parse(body);
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        throwException(call, code, headers, body, e);
        throw e;
    }
}
 
Example 10
Source File: ForumsParser.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
public static String parse(String body) throws ParseException {
    try {
        Document d = Jsoup.parse(body, EhUrl.URL_FORUMS);
        Element userlinks = d.getElementById("userlinks");
        Element child = userlinks.child(0).child(0).child(0);
        return child.attr("href");
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        throw new ParseException("Parse forums error", body);
    }
}
 
Example 11
Source File: GalleryDetailParser.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
/**
 * Parse preview pages with html parser
 */
public static int parsePreviewPages(Document document, String body) throws ParseException {
    try {
        Elements elements = document.getElementsByClass("ptt").first().child(0).child(0).children();
        return Integer.parseInt(elements.get(elements.size() - 2).text());
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        e.printStackTrace();
        throw new ParseException("Can't parse preview pages", body);
    }
}
 
Example 12
Source File: HackyTextInputEditText.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
@Override
public CharSequence getHint() {
  if (HAS_METHOD_UPDATE_CURSOR_POSITION_MZ && FIELD_M_HINT != null) {
    try {
      return getSuperHint();
    } catch (Throwable e) {
      ExceptionUtils.throwIfFatal(e);
      return super.getHint();
    }
  } else {
    return super.getHint();
  }
}
 
Example 13
Source File: SafeCoordinatorLayout.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    try {
        return super.onInterceptTouchEvent(ev);
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        return false;
    }
}
 
Example 14
Source File: GalleryDetailParser.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
@Nullable
private static GalleryTagGroup parseTagGroup(Element element) {
    try {
        GalleryTagGroup group = new GalleryTagGroup();

        String nameSpace = element.child(0).text();
        // Remove last ':'
        nameSpace = nameSpace.substring(0, nameSpace.length() - 1);
        group.groupName = nameSpace;

        Elements tags = element.child(1).children();
        for (int i = 0, n = tags.size(); i < n; i++) {
            String tag = tags.get(i).text();
            // Sometimes parody tag is followed with '|' and english translate, just remove them
            int index = tag.indexOf('|');
            if (index >= 0) {
                tag = tag.substring(0, index).trim();
            }
            group.addTag(tag);
        }

        return group.size() > 0 ? group : null;
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        e.printStackTrace();
        return null;
    }
}
 
Example 15
Source File: SearchEditText.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_UP && mListener != null) {
        mListener.onClick();
    }
    try {
        return super.onTouchEvent(event);
    } catch (Throwable t) {
        // Some devices crash here.
        // I don't why.
        ExceptionUtils.throwIfFatal(t);
        return false;
    }
}
 
Example 16
Source File: EhApplication.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
@Override
public void unbindService(ServiceConnection conn) {
    try {
        super.unbindService(conn);
    } catch (Throwable t) {
        ExceptionUtils.throwIfFatal(t);
    }
}
 
Example 17
Source File: EhEngine.java    From EhViewer with Apache License 2.0 4 votes vote down vote up
private static void doFillGalleryListByApi(@Nullable EhClient.Task task, OkHttpClient okHttpClient,
        List<GalleryInfo> galleryInfoList, String referer) throws Throwable {
    JSONObject json = new JSONObject();
    json.put("method", "gdata");
    JSONArray ja = new JSONArray();
    for (int i = 0, size = galleryInfoList.size(); i < size; i++) {
        GalleryInfo gi = galleryInfoList.get(i);
        JSONArray g = new JSONArray();
        g.put(gi.gid);
        g.put(gi.token);
        ja.put(g);
    }
    json.put("gidlist", ja);
    json.put("namespace", 1);
    String url = EhUrl.getApiUrl();
    String origin = EhUrl.getOrigin();
    Log.d(TAG, url);
    Request request = new EhRequestBuilder(url, referer, origin)
            .post(RequestBody.create(MEDIA_TYPE_JSON, json.toString()))
            .build();
    Call call = okHttpClient.newCall(request);

    // Put call
    if (null != task) {
        task.setCall(call);
    }

    String body = null;
    Headers headers = null;
    int code = -1;
    try {
        Response response = call.execute();
        code = response.code();
        headers = response.headers();
        body = response.body().string();
        GalleryApiParser.parse(body, galleryInfoList);
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        throwException(call, code, headers, body, e);
        throw e;
    }
}
 
Example 18
Source File: GalleryActivity.java    From MHViewer with Apache License 2.0 4 votes vote down vote up
private void shareImage(int page) {
    if (null == mGalleryProvider) {
        return;
    }

    File dir = AppConfig.getExternalTempDir();
    if (null == dir) {
        Toast.makeText(this, R.string.error_cant_create_temp_file, Toast.LENGTH_SHORT).show();
        return;
    }
    UniFile file;
    if (null == (file = mGalleryProvider.save(page, UniFile.fromFile(dir), mGalleryProvider.getImageFilename(page)))) {
        Toast.makeText(this, R.string.error_cant_save_image, Toast.LENGTH_SHORT).show();
        return;
    }
    String filename = file.getName();
    if (filename == null) {
        Toast.makeText(this, R.string.error_cant_save_image, Toast.LENGTH_SHORT).show();
        return;
    }

    String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
            MimeTypeMap.getFileExtensionFromUrl(filename));
    if (TextUtils.isEmpty(mimeType)) {
        mimeType = "image/jpeg";
    }

    Uri uri = new Uri.Builder()
            .scheme(ContentResolver.SCHEME_CONTENT)
            .authority(BuildConfig.FILE_PROVIDER_AUTHORITY)
            .appendPath("temp")
            .appendPath(filename)
            .build();

    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_STREAM, uri);
    intent.setType(mimeType);

    try {
        startActivity(Intent.createChooser(intent, getString(R.string.share_image)));
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        Toast.makeText(this, R.string.error_cant_find_activity, Toast.LENGTH_SHORT).show();
    }
}
 
Example 19
Source File: GalleryActivity.java    From EhViewer with Apache License 2.0 4 votes vote down vote up
private void shareImage(int page) {
    if (null == mGalleryProvider) {
        return;
    }

    File dir = AppConfig.getExternalTempDir();
    if (null == dir) {
        Toast.makeText(this, R.string.error_cant_create_temp_file, Toast.LENGTH_SHORT).show();
        return;
    }
    UniFile file;
    if (null == (file = mGalleryProvider.save(page, UniFile.fromFile(dir), mGalleryProvider.getImageFilename(page)))) {
        Toast.makeText(this, R.string.error_cant_save_image, Toast.LENGTH_SHORT).show();
        return;
    }
    String filename = file.getName();
    if (filename == null) {
        Toast.makeText(this, R.string.error_cant_save_image, Toast.LENGTH_SHORT).show();
        return;
    }

    String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
            MimeTypeMap.getFileExtensionFromUrl(filename));
    if (TextUtils.isEmpty(mimeType)) {
        mimeType = "image/jpeg";
    }

    Uri uri = new Uri.Builder()
            .scheme(ContentResolver.SCHEME_CONTENT)
            .authority(BuildConfig.FILE_PROVIDER_AUTHORITY)
            .appendPath("temp")
            .appendPath(filename)
            .build();

    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_STREAM, uri);
    intent.setType(mimeType);

    try {
        startActivity(Intent.createChooser(intent, getString(R.string.share_image)));
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        Toast.makeText(this, R.string.error_cant_find_activity, Toast.LENGTH_SHORT).show();
    }
}
 
Example 20
Source File: EhEngine.java    From EhViewer with Apache License 2.0 4 votes vote down vote up
/**
 * @param image Must be jpeg
 */
public static GalleryListParser.Result imageSearch(@Nullable EhClient.Task task, OkHttpClient okHttpClient,
        File image, boolean uss, boolean osc, boolean se) throws Throwable {
    MultipartBody.Builder builder = new MultipartBody.Builder();
    builder.setType(MultipartBody.FORM);
    builder.addPart(
            Headers.of("Content-Disposition", "form-data; name=\"sfile\"; filename=\"a.jpg\""),
            RequestBody.create(MEDIA_TYPE_JPEG, image)
    );
    if (uss) {
        builder.addPart(
                Headers.of("Content-Disposition", "form-data; name=\"fs_similar\""),
                RequestBody.create(null, "on")
        );
    }
    if (osc) {
        builder.addPart(
                Headers.of("Content-Disposition", "form-data; name=\"fs_covers\""),
                RequestBody.create(null, "on")
        );
    }
    if (se) {
        builder.addPart(
                Headers.of("Content-Disposition", "form-data; name=\"fs_exp\""),
                RequestBody.create(null, "on")
        );
    }
    builder.addPart(
            Headers.of("Content-Disposition", "form-data; name=\"f_sfile\""),
            RequestBody.create(null, "File Search")
    );
    String url = EhUrl.getImageSearchUrl();
    String referer = EhUrl.getReferer();
    String origin = EhUrl.getOrigin();
    Log.d(TAG, url);
    Request request = new EhRequestBuilder(url, referer, origin)
            .post(builder.build())
            .build();
    Call call = okHttpClient.newCall(request);

    // Put call
    if (null != task) {
        task.setCall(call);
    }

    String body = null;
    Headers headers = null;
    GalleryListParser.Result result;
    int code = -1;
    try {
        Response response = call.execute();

        Log.d(TAG, "" + response.request().url().toString());

        code = response.code();
        headers = response.headers();
        body = response.body().string();
        result = GalleryListParser.parse(body);
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        throwException(call, code, headers, body, e);
        throw e;
    }

    fillGalleryList(task, okHttpClient, result.galleryInfoList, url, true);

    return result;
}