Java源码示例:com.jess.arms.utils.ArmsUtils
示例1
@Override
public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
final AppComponent appComponent = ArmsUtils.obtainAppComponentFromContext(context);
builder.setDiskCache(() -> {
// Careful: the external cache directory doesn't enforce permissions
return DiskLruCacheWrapper.create(DataHelper.makeDirs(new File(appComponent.cacheFile(), "Glide")), IMAGE_DISK_CACHE_MAX_SIZE);
});
MemorySizeCalculator calculator = new MemorySizeCalculator.Builder(context).build();
int defaultMemoryCacheSize = calculator.getMemoryCacheSize();
int defaultBitmapPoolSize = calculator.getBitmapPoolSize();
int customMemoryCacheSize = (int) (1.2 * defaultMemoryCacheSize);
int customBitmapPoolSize = (int) (1.2 * defaultBitmapPoolSize);
builder.setMemoryCache(new LruResourceCache(customMemoryCacheSize));
builder.setBitmapPool(new LruBitmapPool(customBitmapPoolSize));
//将配置 Glide 的机会转交给 GlideImageLoaderStrategy,如你觉得框架提供的 GlideImageLoaderStrategy
//并不能满足自己的需求,想自定义 BaseImageLoaderStrategy,那请你最好实现 GlideAppliesOptions
//因为只有成为 GlideAppliesOptions 的实现类,这里才能调用 applyGlideOptions(),让你具有配置 Glide 的权利
BaseImageLoaderStrategy loadImgStrategy = appComponent.imageLoader().getLoadImgStrategy();
if (loadImgStrategy instanceof GlideAppliesOptions) {
((GlideAppliesOptions) loadImgStrategy).applyGlideOptions(context, builder);
}
}
示例2
@Override
public void handleResponseError(Context context, Throwable t) {
Timber.tag("Catch-Error").w(t.getMessage());
//这里不光只能打印错误, 还可以根据不同的错误做出不同的逻辑处理
//这里只是对几个常用错误进行简单的处理, 展示这个类的用法, 在实际开发中请您自行对更多错误进行更严谨的处理
String msg = "未知错误";
if (t instanceof UnknownHostException) {
msg = "网络不可用";
} else if (t instanceof SocketTimeoutException) {
msg = "请求网络超时";
} else if (t instanceof HttpException) {
HttpException httpException = (HttpException) t;
msg = convertStatusCode(httpException);
} else if (t instanceof JsonParseException || t instanceof ParseException || t instanceof JSONException || t instanceof JsonIOException) {
msg = "数据解析错误";
}
ArmsUtils.snackbarText(msg);
}
示例3
@Override
public void handleResponseError(Context context, Throwable t) {
Timber.tag("Catch-Error").w(t.getMessage());
//这里不光只能打印错误, 还可以根据不同的错误做出不同的逻辑处理
//这里只是对几个常用错误进行简单的处理, 展示这个类的用法, 在实际开发中请您自行对更多错误进行更严谨的处理
String msg = "未知错误";
if (t instanceof UnknownHostException) {
msg = "网络不可用";
} else if (t instanceof SocketTimeoutException) {
msg = "请求网络超时";
} else if (t instanceof HttpException) {
HttpException httpException = (HttpException) t;
msg = convertStatusCode(httpException);
} else if (t instanceof JsonParseException || t instanceof ParseException || t instanceof JSONException || t instanceof JsonIOException) {
msg = "数据解析错误";
}
ArmsUtils.snackbarText(msg);
}
示例4
@Override
public void injectFragmentLifecycle(Context context, List<FragmentManager.FragmentLifecycleCallbacks> lifecycles) {
//当所有模块集成到宿主 App 时, 在 App 中已经执行了以下代码, 所以不需要再执行
if (BuildConfig.IS_BUILD_MODULE) {
lifecycles.add(new FragmentManager.FragmentLifecycleCallbacks() {
@Override
public void onFragmentDestroyed(FragmentManager fm, android.support.v4.app.Fragment f) {
((RefWatcher) ArmsUtils
.obtainAppComponentFromContext(f.getActivity())
.extras()
.get(RefWatcher.class.getName()))
.watch(f);
}
});
}
}
示例5
@Override
public void injectFragmentLifecycle(Context context, List<FragmentManager.FragmentLifecycleCallbacks> lifecycles) {
//当所有模块集成到宿主 App 时, 在 App 中已经执行了以下代码, 所以不需要再执行
if (BuildConfig.IS_BUILD_MODULE) {
lifecycles.add(new FragmentManager.FragmentLifecycleCallbacks() {
@Override
public void onFragmentDestroyed(FragmentManager fm, android.support.v4.app.Fragment f) {
((RefWatcher) ArmsUtils
.obtainAppComponentFromContext(f.getActivity())
.extras()
.get(RefWatcher.class.getName()))
.watch(f);
}
});
}
}
示例6
@Override
public void injectFragmentLifecycle(Context context, List<FragmentManager.FragmentLifecycleCallbacks> lifecycles) {
//当所有模块集成到宿主 App 时, 在 App 中已经执行了以下代码, 所以不需要再执行
if (BuildConfig.IS_BUILD_MODULE) {
lifecycles.add(new FragmentManager.FragmentLifecycleCallbacks() {
@Override
public void onFragmentDestroyed(FragmentManager fm, android.support.v4.app.Fragment f) {
((RefWatcher) ArmsUtils
.obtainAppComponentFromContext(f.getActivity())
.extras()
.get(RefWatcher.class.getName()))
.watch(f);
}
});
}
}
示例7
private void refreshView() {
Cache cache = ArmsUtils.obtainAppComponentFromContext(FollowButton.this.getContext()).extras();
List<MyAttentionEntity> entities = CommonUtils.getFollowedInfo(mContext);
if (entities != null && BmobUser.getCurrentUser() != null) {
boolean isFollowed = false;
for (MyAttentionEntity entity : entities) {
if (entity.getId() == this.attention.getId()) {
isFollowed = true;
}
}
if (isFollowed) {
setState(FOLLOWED);
} else {
setState(UNFOLLOWED);
}
}
}
示例8
private void initOtherInfo(Summary.DataBean data) {
List<Summary.DataBean.RelatesBean> relates = data.getRelates();
if (relates != null) {
mRvRelates.setLayoutManager(new LinearLayoutManager(_mActivity));
mRvRelates.setAdapter(new BaseQuickAdapter<Summary.DataBean.RelatesBean, BaseViewHolder>(R.layout.item_relate_video_detail, relates) {
@Override
protected void convert(BaseViewHolder helper, Summary.DataBean.RelatesBean item) {
ArmsUtils.obtainAppComponentFromContext(_mActivity).imageLoader().loadImage(_mActivity, ImageConfigImpl.builder().url(item.getPic()).imageView(helper.getView(R.id.iv_pic)).build());
helper.setText(R.id.tv_title, item.getTitle())
.setText(R.id.tv_owner_name, item.getOwner() == null ? "" : item.getOwner().getName())
.setText(R.id.tv_view, item.getStat() == null ? "0" : TextHandleUtil.handleCount2TenThousand(item.getStat().getView()))
.setText(R.id.tv_danmaku, item.getStat() == null ? "0" : TextHandleUtil.handleCount2TenThousand(item.getStat().getDanmaku()));
}
});
}
}
示例9
private void showVideoStartTip() {
mRlVideoTip.setVisibility(View.VISIBLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Animator circularReveal = ViewAnimationUtils.createCircularReveal(mRlVideoTip,
mIvCover.getWidth() - ArmsUtils.dip2px(VideoDetailActivity.this, mAnchorX),
mIvCover.getHeight() - ArmsUtils.dip2px(VideoDetailActivity.this, mAnchorY),
0,
((float) Math.hypot(mIvCover.getWidth(), mIvCover.getHeight())));
circularReveal.setDuration(800);
circularReveal.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
mIvCover.setVisibility(View.GONE);
mPresenter.loadPlayUrl(aid);
}
});
circularReveal.start();
} else {
mPresenter.loadPlayUrl(aid);
}
// 锁定AppBarLayout
AppBarLayout.LayoutParams layoutParams = (AppBarLayout.LayoutParams) mAppbar.getChildAt(0).getLayoutParams();
layoutParams.setScrollFlags(0);
mAppbar.getChildAt(0).setLayoutParams(layoutParams);
}
示例10
@Override
public void onBackPressedSupport() {
if (mDrawer.isDrawerOpen(GravityCompat.START)) {
closeDrawer();
} else {
ISupportFragment topFragment = getTopFragment();
if (!(topFragment instanceof NavHomeFragment)) {
mNav.setCheckedItem(R.id.nav_home);
}
if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
pop();
} else {
// 放置后台
// moveTaskToBack(false);
// 2秒内两次点击返回键退出应用
long nowTime = System.currentTimeMillis();
if (nowTime - mPreTime > 2000) {
ArmsUtils.makeText(this, ArmsUtils.getString(this, R.string.double_click_to_exit));
mPreTime = nowTime;
} else {
ArmsUtils.exitApp();
}
}
}
}
示例11
@Override
public void handleResponseError(Context context, Throwable t) {
Timber.tag("Catch-Error").w(t);
//这里不光只能打印错误, 还可以根据不同的错误做出不同的逻辑处理
//这里只是对几个常用错误进行简单的处理, 展示这个类的用法, 在实际开发中请您自行对更多错误进行更严谨的处理
String msg = "未知错误";
if (t instanceof UnknownHostException) {
msg = "网络不可用";
} else if (t instanceof SocketTimeoutException) {
msg = "请求网络超时";
} else if (t instanceof HttpException) {
HttpException httpException = (HttpException) t;
msg = convertStatusCode(httpException);
} else if (t instanceof JsonParseException || t instanceof ParseException || t instanceof JSONException || t instanceof JsonIOException) {
msg = "数据解析错误";
}
ArmsUtils.snackbarText(msg);
}
示例12
@Override
public void onBackPressed() {
//获取第一次按键时间
long mNowTime = System.currentTimeMillis();
//比较两次按键时间差
if ((mNowTime - mPressedTime) > 2000) {
ArmsUtils.makeText(getApplicationContext(),
"再按一次退出" + ArmsUtils.getString(getApplicationContext(), R.string.public_app_name));
mPressedTime = mNowTime;
} else {
super.onBackPressed();
}
}
示例13
@Override
public void onCreate(@NonNull Application application) {
if (LeakCanary.isInAnalyzerProcess(application)) {
// This process is dedicated to LeakCanary for heap analysis.
// You should not init your app in this process.
return;
}
//leakCanary内存泄露检查
ArmsUtils.obtainAppComponentFromContext(application).extras().put(RefWatcher.class.getName(), BuildConfig.USE_CANARY ? LeakCanary.install(application) : RefWatcher.DISABLED);
}
示例14
@Override
public void injectFragmentLifecycle(Context context, List<FragmentManager.FragmentLifecycleCallbacks> lifecycles) {
lifecycles.add(new FragmentManager.FragmentLifecycleCallbacks() {
@Override
public void onFragmentDestroyed(FragmentManager fm, Fragment f) {
((RefWatcher) ArmsUtils
.obtainAppComponentFromContext(f.getActivity())
.extras()
.get(RefWatcher.class.getName()))
.watch(f);
}
});
}
示例15
@Override
public void onActivityStarted(Activity activity) {
Timber.i(activity + " - onActivityStarted");
if (!activity.getIntent().getBooleanExtra("isInitToolbar", false)) {
//由于加强框架的兼容性,故将 setContentView 放到 onActivityCreated 之后,onActivityStarted 之前执行
//而 findViewById 必须在 Activity setContentView() 后才有效,所以将以下代码从之前的 onActivityCreated 中移动到 onActivityStarted 中执行
activity.getIntent().putExtra("isInitToolbar", true);
//这里全局给Activity设置toolbar和title,你想象力有多丰富,这里就有多强大,以前放到BaseActivity的操作都可以放到这里
if (ArmsUtils.findViewByName(activity.getApplicationContext(), activity, "public_toolbar") != null) {
if (activity instanceof AppCompatActivity) {
((AppCompatActivity) activity).setSupportActionBar(ArmsUtils.findViewByName(activity.getApplicationContext(), activity, "public_toolbar"));
((AppCompatActivity) activity).getSupportActionBar().setDisplayShowTitleEnabled(false);
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
activity.setActionBar(ArmsUtils.findViewByName(activity.getApplicationContext(), activity, "public_toolbar"));
activity.getActionBar().setDisplayShowTitleEnabled(false);
}
}
}
if (ArmsUtils.findViewByName(activity.getApplicationContext(), activity, "public_toolbar_title") != null) {
((TextView) ArmsUtils.findViewByName(activity.getApplicationContext(), activity, "public_toolbar_title")).setText(activity.getTitle());
}
if (ArmsUtils.findViewByName(activity.getApplicationContext(), activity, "public_toolbar_back") != null) {
ArmsUtils.findViewByName(activity.getApplicationContext(), activity, "public_toolbar_back").setOnClickListener(v -> {
activity.onBackPressed();
});
}
}
}
示例16
@Override
public void injectFragmentLifecycle(Context context, List<FragmentManager.FragmentLifecycleCallbacks> lifecycles) {
lifecycles.add(new FragmentManager.FragmentLifecycleCallbacks() {
@Override
public void onFragmentDestroyed(FragmentManager fm, Fragment f) {
((RefWatcher) ArmsUtils
.obtainAppComponentFromContext(f.getActivity())
.extras()
.get(RefWatcher.class.getName()))
.watch(f);
}
});
}
示例17
@Override
public void onActivityStarted(Activity activity) {
Timber.w(activity + " - onActivityStarted");
if (!activity.getIntent().getBooleanExtra("isInitToolbar", false)) {
//由于加强框架的兼容性,故将 setContentView 放到 onActivityCreated 之后,onActivityStarted 之前执行
//而 findViewById 必须在 Activity setContentView() 后才有效,所以将以下代码从之前的 onActivityCreated 中移动到 onActivityStarted 中执行
activity.getIntent().putExtra("isInitToolbar", true);
//这里全局给Activity设置toolbar和title,你想象力有多丰富,这里就有多强大,以前放到BaseActivity的操作都可以放到这里
if (ArmsUtils.findViewByName(activity.getApplicationContext(), activity, "public_toolbar") != null) {
if (activity instanceof AppCompatActivity) {
((AppCompatActivity) activity).setSupportActionBar(ArmsUtils.findViewByName(activity.getApplicationContext(), activity, "public_toolbar"));
((AppCompatActivity) activity).getSupportActionBar().setDisplayShowTitleEnabled(false);
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
activity.setActionBar(ArmsUtils.findViewByName(activity.getApplicationContext(), activity, "public_toolbar"));
activity.getActionBar().setDisplayShowTitleEnabled(false);
}
}
}
if (ArmsUtils.findViewByName(activity.getApplicationContext(), activity, "public_toolbar_title") != null) {
((TextView) ArmsUtils.findViewByName(activity.getApplicationContext(), activity, "public_toolbar_title")).setText(activity.getTitle());
}
if (ArmsUtils.findViewByName(activity.getApplicationContext(), activity, "public_toolbar_back") != null) {
ArmsUtils.findViewByName(activity.getApplicationContext(), activity, "public_toolbar_back").setOnClickListener(v -> {
activity.onBackPressed();
});
}
}
}
示例18
@NonNull
@Override
public synchronized Cache<String, Object> provideCache() {
if (mCache == null) {
//noinspection unchecked
mCache = ArmsUtils.obtainAppComponentFromContext(getActivity()).cacheFactory().build(CacheType.FRAGMENT_CACHE);
}
return mCache;
}
示例19
@Override
public void onCreate(Application application) {
if (LeakCanary.isInAnalyzerProcess(application)) {
// This process is dedicated to LeakCanary for heap analysis.
// You should not init your app in this process.
return;
}
//使用 RetrofitUrlManager 切换 BaseUrl
//RetrofitUrlManager.getInstance().putDomain(STEAM_DOMAIN_NAME, STEAM_DOMAIN);
//当所有模块集成到宿主 App 时, 在 App 中已经执行了以下代码
if (BuildConfig.IS_BUILD_MODULE) {
//leakCanary内存泄露检查
ArmsUtils.obtainAppComponentFromContext(application).extras().put(RefWatcher.class.getName(), BuildConfig.USE_CANARY ? LeakCanary.install(application) : RefWatcher.DISABLED);
}
}
示例20
@Override
public void onBackPressed() {
//获取第一次按键时间
long mNowTime = System.currentTimeMillis();
//比较两次按键时间差
if ((mNowTime - mPressedTime) > 2000) {
ArmsUtils.makeText(getApplicationContext(),
"再按一次退出" + ArmsUtils.getString(getApplicationContext(), R.string.public_app_name));
mPressedTime = mNowTime;
} else {
super.onBackPressed();
}
}
示例21
@Override
public void onCreate(@NonNull Application application) {
if (LeakCanary.isInAnalyzerProcess(application)) {
// This process is dedicated to LeakCanary for heap analysis.
// You should not init your app in this process.
return;
}
//leakCanary内存泄露检查
ArmsUtils.obtainAppComponentFromContext(application).extras().put(RefWatcher.class.getName(), BuildConfig.USE_CANARY ? LeakCanary.install(application) : RefWatcher.DISABLED);
}
示例22
@Override
public void injectFragmentLifecycle(Context context, List<FragmentManager.FragmentLifecycleCallbacks> lifecycles) {
lifecycles.add(new FragmentManager.FragmentLifecycleCallbacks() {
@Override
public void onFragmentDestroyed(FragmentManager fm, Fragment f) {
((RefWatcher) ArmsUtils
.obtainAppComponentFromContext(f.getActivity())
.extras()
.get(RefWatcher.class.getName()))
.watch(f);
}
});
}
示例23
public SaleHolder(View itemView) {
super(itemView);
mAppComponent= ArmsUtils.obtainAppComponentFromContext(itemView.getContext());
mImageLoader=mAppComponent.imageLoader();
imageView=(ImageView)itemView.findViewById(R.id.img);
titleTv=(TextView)itemView.findViewById(R.id.name);
nowpriceTv=(TextView)itemView.findViewById(R.id.nowprice);
oldpriceTv=(TextView)itemView.findViewById(R.id.oldprice);
offTv=(TextView)itemView.findViewById(R.id.off);
}
示例24
@Override
public void onCreate(Application application) {
if (LeakCanary.isInAnalyzerProcess(application)) {
// This process is dedicated to LeakCanary for heap analysis.
// You should not init your app in this process.
return;
}
//使用 RetrofitUrlManager 切换 BaseUrl
//RetrofitUrlManager.getInstance().putDomain(STEAM_DOMAIN_NAME, STEAM_DOMAIN);
//当所有模块集成到宿主 App 时, 在 App 中已经执行了以下代码
if (BuildConfig.IS_BUILD_MODULE) {
//leakCanary内存泄露检查
ArmsUtils.obtainAppComponentFromContext(application).extras().put(RefWatcher.class.getName(), BuildConfig.USE_CANARY ? LeakCanary.install(application) : RefWatcher.DISABLED);
}
}
示例25
public SearchHolder(View itemView) {
super(itemView);
mAppComponent= ArmsUtils.obtainAppComponentFromContext(itemView.getContext());
mImageLoader=mAppComponent.imageLoader();
imageView=(ImageView)itemView.findViewById(R.id.img);
titleTv=(TextView)itemView.findViewById(R.id.name);
nowpriceTv=(TextView)itemView.findViewById(R.id.nowprice);
}
示例26
@Override
public void onCreate(Application application) {
if (LeakCanary.isInAnalyzerProcess(application)) {
// This process is dedicated to LeakCanary for heap analysis.
// You should not init your app in this process.
return;
}
//使用 RetrofitUrlManager 切换 BaseUrl
//RetrofitUrlManager.getInstance().putDomain(STEAM_DOMAIN_NAME, STEAM_DOMAIN);
//当所有模块集成到宿主 App 时, 在 App 中已经执行了以下代码
if (BuildConfig.IS_BUILD_MODULE) {
//leakCanary内存泄露检查
ArmsUtils.obtainAppComponentFromContext(application).extras().put(RefWatcher.class.getName(), BuildConfig.USE_CANARY ? LeakCanary.install(application) : RefWatcher.DISABLED);
}
}
示例27
@NonNull
@Override
public synchronized Cache<String, Object> provideCache() {
if (mCache == null) {
mCache = ArmsUtils.obtainAppComponentFromContext(getActivity()).cacheFactory().build(CacheType.FRAGMENT_CACHE);
}
return mCache;
}
示例28
@NonNull
@Override
public synchronized Cache<String, Object> provideCache() {
if (mCache == null) {
mCache = ArmsUtils.obtainAppComponentFromContext(this).cacheFactory().build(CacheType.ACTIVITY_CACHE);
}
return mCache;
}
示例29
@NonNull
@Override
public synchronized Cache<String, Object> provideCache() {
if (mCache == null) {
mCache = ArmsUtils.obtainAppComponentFromContext(getActivity()).cacheFactory().build(CacheType.FRAGMENT_CACHE);
}
return mCache;
}
示例30
@Override
public void applyOptions(Context context, GlideBuilder builder) {
AppComponent appComponent = ArmsUtils.obtainAppComponentFromContext(context);
builder.setDiskCache(new DiskCache.Factory() {
@Override
public DiskCache build() {
// Careful: the external cache directory doesn't enforce permissions
return DiskLruCacheWrapper.get(DataHelper.makeDirs(new File(appComponent.cacheFile(), "Glide")), IMAGE_DISK_CACHE_MAX_SIZE);
}
});
MemorySizeCalculator calculator = new MemorySizeCalculator.Builder(context).build();
int defaultMemoryCacheSize = calculator.getMemoryCacheSize();
int defaultBitmapPoolSize = calculator.getBitmapPoolSize();
int customMemoryCacheSize = (int) (1.2 * defaultMemoryCacheSize);
int customBitmapPoolSize = (int) (1.2 * defaultBitmapPoolSize);
builder.setMemoryCache(new LruResourceCache(customMemoryCacheSize));
builder.setBitmapPool(new LruBitmapPool(customBitmapPoolSize));
//将配置 Glide 的机会转交给 GlideImageLoaderStrategy,如你觉得框架提供的 GlideImageLoaderStrategy
//并不能满足自己的需求,想自定义 BaseImageLoaderStrategy,那请你最好实现 GlideAppliesOptions
//因为只有成为 GlideAppliesOptions 的实现类,这里才能调用 applyGlideOptions(),让你具有配置 Glide 的权利
BaseImageLoaderStrategy loadImgStrategy = appComponent.imageLoader().getLoadImgStrategy();
if (loadImgStrategy instanceof GlideAppliesOptions) {
((GlideAppliesOptions) loadImgStrategy).applyGlideOptions(context, builder);
}
}