Search in sources :

Example 31 with TaskException

use of org.aisen.android.network.task.TaskException in project AisenWeiBo by wangdan.

the class SinaSDK method webGetHotTopicsHotStatus.

/**
 * 热门话题的推荐推荐微博列表
 *
 * @param containerId
 * @param sinceId
 * @return
 * @throws TaskException
 */
public StatusContents webGetHotTopicsHotStatus(String containerId, String sinceId) throws TaskException {
    Params params = new Params();
    params.addParameter("containerid", containerId);
    if (!TextUtils.isEmpty(sinceId)) {
        params.addParameter("since_id", sinceId);
    }
    Setting action = newSetting("webGetHotTopicsHotStatus", "container/getIndex", "热门话题热门微博");
    action.getExtras().put(HTTP_UTILITY, newSettingExtra(HTTP_UTILITY, TimelineHotTopicsHttpUtility.class.getName(), ""));
    HttpConfig config = webConfig();
    config.addHeader("Referer", String.format("http://m.weibo.cn/p/index?containerid=%s", containerId));
    try {
        return doGet(config, action, params, StatusContents.class);
    } catch (Exception e) {
        if (e instanceof TaskException)
            checkWebResult((TaskException) e);
        throw e;
    }
}
Also used : TaskException(org.aisen.android.network.task.TaskException) Setting(org.aisen.android.common.setting.Setting) Params(org.aisen.android.network.http.Params) HttpConfig(org.aisen.android.network.http.HttpConfig) ParseException(java.text.ParseException) TaskException(org.aisen.android.network.task.TaskException)

Example 32 with TaskException

use of org.aisen.android.network.task.TaskException in project AisenWeiBo by wangdan.

the class WallpaperDownloadTask method saveWallpaper.

// 保存壁纸到相册
private File saveWallpaper(File file) throws TaskException {
    File savedFile = getWallpaperSaveFile(mImageUrl);
    if (savedFile.exists())
        return savedFile;
    if (file.exists()) {
        try {
            FileUtils.copyFile(file, savedFile);
            Logger.v("保存成功!:" + savedFile.getAbsolutePath());
            if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
                // 解决在部分机器缓存更新不及时问题
                mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
            }
            try {
                Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                Uri uri = Uri.fromFile(savedFile);
                intent.setData(uri);
                mContext.sendBroadcast(intent);
            } catch (Exception e) {
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return savedFile;
    }
    return null;
}
Also used : Intent(android.content.Intent) File(java.io.File) Uri(android.net.Uri) TaskException(org.aisen.android.network.task.TaskException) IOException(java.io.IOException)

Example 33 with TaskException

use of org.aisen.android.network.task.TaskException in project AisenWeiBo by wangdan.

the class WallpaperDownloadTask method setWallpaper.

// 设置壁纸
public static void setWallpaper(Context context, File file, final OnProgressCallback callback) throws TaskException {
    long time = System.currentTimeMillis();
    try {
        WallpaperManager wallpaperManager = WallpaperManager.getInstance(GlobalContext.getInstance());
        try {
            DisplayMetrics dm = new DisplayMetrics();
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            windowManager.getDefaultDisplay().getMetrics(dm);
            int width = dm.widthPixels;
            int height = dm.heightPixels;
            int navigationBarHeight = SystemUtils.getNavigationBarHeight(context);
            int wallpaperWidth = width;
            int wallpaperHeight = height;
            Options opts = new Options();
            opts.inJustDecodeBounds = true;
            Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), opts);
            boolean decode = false;
            if (opts.outHeight > height + navigationBarHeight) {
                decode = true;
            }
            Logger.d(TAG, "image height = %d, screen height = %d", opts.outHeight, height + navigationBarHeight);
            if (decode) {
                // docode的时间会稍微长一点,idol3测试在1S内,所以先显示一个99%的进度
                // if (progress <= 0) {
                // progress = 999l;
                // total = 1000l;
                // publishProgress(progress, total);
                // }
                opts.inJustDecodeBounds = false;
                bitmap = BitmapFactory.decodeStream(new FileInputStream(file));
                Matrix matrix = new Matrix();
                float scale = wallpaperHeight * 1.0f / bitmap.getHeight();
                matrix.setScale(scale, scale);
                bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
                ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
                wallpaperManager.setStream(in);
                if (callback != null) {
                    new Handler(Looper.getMainLooper()).post(new Runnable() {

                        @Override
                        public void run() {
                            callback.onSetWallpaper(true);
                        }
                    });
                }
                Logger.d(TAG, "设置处理后的壁纸耗时 : " + (System.currentTimeMillis() - time));
                return;
            }
        } catch (Throwable e) {
            Logger.printExc(WallpaperDownloadTask.class, e);
        }
        wallpaperManager.setStream(new FileInputStream(file));
        Logger.d(TAG, "设置原壁纸耗时 : " + (System.currentTimeMillis() - time));
        if (callback != null) {
            new Handler(Looper.getMainLooper()).post(new Runnable() {

                @Override
                public void run() {
                    callback.onSetWallpaper(true);
                }
            });
        }
    } catch (Exception e) {
        Logger.printExc(WallpaperDownloadTask.class, e);
        if (callback != null) {
            new Handler(Looper.getMainLooper()).post(new Runnable() {

                @Override
                public void run() {
                    callback.onSetWallpaper(false);
                }
            });
        }
        throw new TaskException("", context.getResources().getString(R.string.txt_set_wallpaper_fail));
    }
}
Also used : Options(android.graphics.BitmapFactory.Options) Handler(android.os.Handler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DisplayMetrics(android.util.DisplayMetrics) FileInputStream(java.io.FileInputStream) TaskException(org.aisen.android.network.task.TaskException) IOException(java.io.IOException) WindowManager(android.view.WindowManager) Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) TaskException(org.aisen.android.network.task.TaskException) ByteArrayInputStream(java.io.ByteArrayInputStream) WallpaperManager(android.app.WallpaperManager)

Example 34 with TaskException

use of org.aisen.android.network.task.TaskException in project AisenWeiBo by wangdan.

the class APublishFragment method choieUri.

@Override
public void choieUri(Uri uri, int requestCode) {
    Logger.e(uri.toString());
    // 当拍摄照片时,提示是否设置旋转90度
    if (!AppSettings.isRotatePic() && !ActivityHelper.getBooleanShareData(GlobalContext.getInstance(), "RotatePicNoRemind", false)) {
        new MaterialDialog.Builder(getActivity()).title(R.string.remind).content(R.string.publish_rotate_remind).negativeText(R.string.donnot_remind).onNegative(new MaterialDialog.SingleButtonCallback() {

            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                ActivityHelper.putBooleanShareData(GlobalContext.getInstance(), "RotatePicNoRemind", true);
            }
        }).positiveText(R.string.i_know).show();
    }
    // 拍摄照片时,顺时针旋转90度
    if (requestCode == PhotoChoice.CAMERA_IMAGE_REQUEST_CODE && AppSettings.isRotatePic()) {
        final String path = uri.toString().replace("file://", "");
        new WorkTask<Void, Void, String>() {

            @Override
            public String workInBackground(Void... params) throws TaskException {
                try {
                    Bitmap bitmap = BitmapDecoder.decodeSampledBitmapFromFile(path, SystemUtils.getScreenHeight(getActivity()), SystemUtils.getScreenHeight(getActivity()));
                    bitmap = BitmapUtil.rotateBitmap(bitmap, 90);
                    ByteArrayOutputStream outArray = new ByteArrayOutputStream();
                    bitmap.compress(CompressFormat.JPEG, 100, outArray);
                    FileUtils.writeFile(new File(path), outArray.toByteArray());
                } catch (OutOfMemoryError e) {
                    e.printStackTrace();
                }
                return path;
            }

            protected void onSuccess(String result) {
                setPicUri(result);
            }
        }.execute();
    } else {
        setPicUri(uri.toString());
    }
}
Also used : MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) SpannableString(android.text.SpannableString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Bitmap(android.graphics.Bitmap) TaskException(org.aisen.android.network.task.TaskException) DialogAction(com.afollestad.materialdialogs.DialogAction) File(java.io.File)

Example 35 with TaskException

use of org.aisen.android.network.task.TaskException in project AisenWeiBo by wangdan.

the class CacheClearFragment method clearCache.

private void clearCache(final boolean all) {
    UMengUtil.onEvent(getActivity(), all ? "clear_cache_all" : "clear_cache_outofdate");
    final WorkTask<Void, String, Void> task = new WorkTask<Void, String, Void>() {

        @Override
        public Void workInBackground(Void... params) throws TaskException {
            File cacheRootFile = new File(cachePath);
            deleteFile(cacheRootFile, all);
            return null;
        }

        void deleteFile(File file, boolean all) {
            if (!file.exists())
                return;
            if (!isCancelled()) {
                if (file.isDirectory()) {
                    File[] childFiles = file.listFiles();
                    for (File childFile : childFiles) deleteFile(childFile, all);
                } else {
                    publishProgress(String.valueOf(file.length()));
                    boolean clear = all;
                    if (!clear) {
                        Logger.v("ClearCache", String.format("文件最后修改时间是%s", DateUtils.formatDate(file.lastModified(), DateUtils.TYPE_01)));
                        clear = System.currentTimeMillis() - file.lastModified() >= RETAIN_TIME;
                        if (clear)
                            Logger.v("ClearCache", "缓存超过2天,删除该缓存");
                    } else {
                        file.delete();
                    }
                // if (clear && file.delete())
                // SystemUtils.scanPhoto(file);
                }
            }
        }

        @Override
        protected void onProgressUpdate(String... values) {
            super.onProgressUpdate(values);
            if (values != null && values.length > 0 && materialDialog != null && getActivity() != null) {
                int value = Integer.parseInt(values[0]);
                // materialDialog.incrementProgress(value / 1024);
                materialDialog.incrementProgress(1);
            // if (value * 1.0f / 1024 / 1024 > 1)
            // materialDialog.setContent(String.format("%s M", new DecimalFormat("#.00").format(value * 1.0d / 1024 / 1024)));
            // else
            // materialDialog.setContent(String.format("%d Kb", value / 1024));
            }
        }

        protected void onFinished() {
            if (materialDialog != null && materialDialog.isShowing())
                materialDialog.dismiss();
        }
    }.execute();
    materialDialog = new MaterialDialog.Builder(getActivity()).title(R.string.settings_cache_clear).contentGravity(GravityEnum.CENTER).dismissListener(new DialogInterface.OnDismissListener() {

        @Override
        public void onDismiss(DialogInterface dialog) {
            task.cancel(true);
            calculateCacheFileSize();
        }
    }).positiveText(R.string.cancel).progress(false, cacheCount, true).show();
}
Also used : MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) TaskException(org.aisen.android.network.task.TaskException) DialogInterface(android.content.DialogInterface) File(java.io.File)

Aggregations

TaskException (org.aisen.android.network.task.TaskException)45 Setting (org.aisen.android.common.setting.Setting)11 Params (org.aisen.android.network.http.Params)10 File (java.io.File)9 HttpConfig (org.aisen.android.network.http.HttpConfig)8 WeiBoUser (org.aisen.weibo.sina.sinasdk.bean.WeiBoUser)8 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)7 DialogAction (com.afollestad.materialdialogs.DialogAction)6 JSONArray (com.alibaba.fastjson.JSONArray)6 JSONObject (com.alibaba.fastjson.JSONObject)6 ParseException (java.text.ParseException)6 Bitmap (android.graphics.Bitmap)5 ImageView (android.widget.ImageView)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 IOException (java.io.IOException)4 View (android.view.View)3 Request (com.squareup.okhttp.Request)3 Response (com.squareup.okhttp.Response)3 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3