Here are the examples of the java api android.app.Application taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
1831 Examples
19
Source : AppModule.java
with GNU General Public License v3.0
from zyl409214686
with GNU General Public License v3.0
from zyl409214686
/**
* Description: com.zyl.mp3cutter.common.app.di
* Created by zouyulong on 2017/11/7.
* Phone : 15810880928
* Person in charge : zouyulong
*/
@Module
public clreplaced AppModule {
private Application mApplication;
public AppModule(Application application) {
this.mApplication = application;
}
@Singleton
@Provides
public Application provideApplication() {
return mApplication;
}
}
19
Source : RepositoryManager.java
with Apache License 2.0
from Zweihui
with Apache License 2.0
from Zweihui
/**
* ================================================
* 用来管理网络请求层,以及数据缓存层,以后可能添加数据库请求层
* 提供给 {@link IModel} 层必要的 Api 做数据处理
*
* @see <a href="https://github.com/JessYanCoding/MVPArms/wiki#2.3">RepositoryManager wiki 官方文档</a>
* Created by JessYan on 13/04/2017 09:52
* <a href="mailto:[email protected]">Contact me</a>
* <a href="https://github.com/JessYanCoding">Follow me</a>
* ================================================
*/
@Singleton
public clreplaced RepositoryManager implements IRepositoryManager {
private Lazy<Retrofit> mRetrofit;
private Lazy<RxCache> mRxCache;
private Application mApplication;
private Cache<String, Object> mRetrofitServiceCache;
private Cache<String, Object> mCacheServiceCache;
private Cache.Factory mCachefactory;
@Inject
public RepositoryManager(Lazy<Retrofit> retrofit, Lazy<RxCache> rxCache, Application application, Cache.Factory cachefactory) {
this.mRetrofit = retrofit;
this.mRxCache = rxCache;
this.mApplication = application;
this.mCachefactory = cachefactory;
}
/**
* 根据传入的 Clreplaced 获取对应的 Retrofit service
*
* @param service
* @param <T>
* @return
*/
@Override
public synchronized <T> T obtainRetrofitService(Clreplaced<T> service) {
if (mRetrofitServiceCache == null)
mRetrofitServiceCache = mCachefactory.build(CacheType.RETROFIT_SERVICE_CACHE);
Preconditions.checkNotNull(mRetrofitServiceCache, "Cannot return null from a Cache.Factory#build(int) method");
T retrofitService = (T) mRetrofitServiceCache.get(service.getCanonicalName());
if (retrofitService == null) {
retrofitService = mRetrofit.get().create(service);
mRetrofitServiceCache.put(service.getCanonicalName(), retrofitService);
}
return retrofitService;
}
/**
* 根据传入的 Clreplaced 获取对应的 RxCache service
*
* @param cache
* @param <T>
* @return
*/
@Override
public synchronized <T> T obtainCacheService(Clreplaced<T> cache) {
if (mCacheServiceCache == null)
mCacheServiceCache = mCachefactory.build(CacheType.CACHE_SERVICE_CACHE);
Preconditions.checkNotNull(mCacheServiceCache, "Cannot return null from a Cache.Factory#build(int) method");
T cacheService = (T) mCacheServiceCache.get(cache.getCanonicalName());
if (cacheService == null) {
cacheService = mRxCache.get().using(cache);
mCacheServiceCache.put(cache.getCanonicalName(), cacheService);
}
return cacheService;
}
/**
* 清理所有缓存
*/
@Override
public void clearAllCache() {
mRxCache.get().evictAll();
}
@Override
public Context getContext() {
return mApplication;
}
}
19
Source : AppModule.java
with Apache License 2.0
from Zweihui
with Apache License 2.0
from Zweihui
/**
* ================================================
* 提供一些框架必须的实例的 {@link Module}
* <p>
* Created by JessYan on 8/4/2016.
* <a href="mailto:[email protected]">Contact me</a>
* <a href="https://github.com/JessYanCoding">Follow me</a>
* ================================================
*/
@Module
public clreplaced AppModule {
private Application mApplication;
public AppModule(Application application) {
this.mApplication = application;
}
@Singleton
@Provides
public Application provideApplication() {
return mApplication;
}
@Singleton
@Provides
public Gson provideGson(Application application, @Nullable GsonConfiguration configuration) {
GsonBuilder builder = new GsonBuilder();
if (configuration != null)
configuration.configGson(application, builder);
return builder.create();
}
@Singleton
@Provides
public IRepositoryManager provideRepositoryManager(RepositoryManager repositoryManager) {
return repositoryManager;
}
@Singleton
@Provides
public Cache<String, Object> provideExtras(Cache.Factory cacheFactory) {
return cacheFactory.build(CacheType.EXTRAS);
}
public interface GsonConfiguration {
void configGson(Context context, GsonBuilder builder);
}
}
19
Source : AppDelegate.java
with Apache License 2.0
from Zweihui
with Apache License 2.0
from Zweihui
@Override
public void onTerminate(Application application) {
if (mActivityLifecycle != null) {
mApplication.unregisterActivityLifecycleCallbacks(mActivityLifecycle);
}
if (mActivityLifecycleForRxLifecycle != null) {
mApplication.unregisterActivityLifecycleCallbacks(mActivityLifecycleForRxLifecycle);
}
if (mComponentCallback != null) {
mApplication.unregisterComponentCallbacks(mComponentCallback);
}
if (mActivityLifecycles != null && mActivityLifecycles.size() > 0) {
for (Application.ActivityLifecycleCallbacks lifecycle : mActivityLifecycles) {
mApplication.unregisterActivityLifecycleCallbacks(lifecycle);
}
}
if (mAppLifecycles != null && mAppLifecycles.size() > 0) {
for (AppLifecycles lifecycle : mAppLifecycles) {
lifecycle.onTerminate(mApplication);
}
}
this.mAppComponent = null;
this.mActivityLifecycle = null;
this.mActivityLifecycleForRxLifecycle = null;
this.mActivityLifecycles = null;
this.mComponentCallback = null;
this.mAppLifecycles = null;
this.mApplication = null;
}
19
Source : VideoModel.java
with Apache License 2.0
from Zweihui
with Apache License 2.0
from Zweihui
@ActivityScope
public clreplaced VideoModel extends BaseModel implements VideoContract.Model {
private Gson mGson;
private Application mApplication;
@Inject
public VideoModel(IRepositoryManager repositoryManager, Gson gson, Application application) {
super(repositoryManager);
this.mGson = gson;
this.mApplication = application;
}
@Override
public void onDestroy() {
super.onDestroy();
this.mGson = null;
this.mApplication = null;
}
@Override
public Observable<IndextVideoListInfo> getIndexVideoList(long date, int num) {
Observable<IndextVideoListInfo> observable = mRepositoryManager.obtainRetrofitService(VideoService.clreplaced).getIndexVideoList(date, num, Constants.UDID, Constants.VC, Constants.VN, Constants.DEVICEMODEL);
return observable;
}
@Override
public Observable<IndextVideoListInfo> getMoreIndexVideoList(int page) {
Observable<IndextVideoListInfo> observable = mRepositoryManager.obtainRetrofitService(VideoService.clreplaced).getMoreIndexVideoList(2, page, Constants.UDID, Constants.VC, Constants.VN, Constants.DEVICEMODEL);
return observable;
}
@Override
public Observable<VideoListInfo> getVideoList(String type, String lastIdQueried, int startCount, boolean update) {
Observable<VideoListInfo> videoInfo = mRepositoryManager.obtainRetrofitService(VideoService.clreplaced).getVideoList(startCount, Constants.HOME_VIDEO_LIST_PAGE_SIZE, type, Constants.UDID);
// 使用rxcache缓存,上拉刷新则不读取缓存,加载更多读取缓存
return mRepositoryManager.obtainCacheService(CommonCache.clreplaced).getVideoList(videoInfo, new DynamicKey(lastIdQueried), new EvictDynamicKey(update)).flatMap(new Function<Reply<VideoListInfo>, ObservableSource<VideoListInfo>>() {
@Override
public ObservableSource<VideoListInfo> apply(@NonNull Reply<VideoListInfo> listReply) throws Exception {
return Observable.just(listReply.getData());
}
});
}
}
19
Source : VideoListActivityModel.java
with Apache License 2.0
from Zweihui
with Apache License 2.0
from Zweihui
@ActivityScope
public clreplaced VideoListActivityModel extends BaseModel implements VideoListActivityContract.Model {
private Gson mGson;
private Application mApplication;
@Inject
public VideoListActivityModel(IRepositoryManager repositoryManager, Gson gson, Application application) {
super(repositoryManager);
this.mGson = gson;
this.mApplication = application;
}
@Override
public void onDestroy() {
super.onDestroy();
this.mGson = null;
this.mApplication = null;
}
@Override
public Observable<VideoListInfo> getPopularVideoList(int id, String strategy, int start) {
Observable<VideoListInfo> observable = mRepositoryManager.obtainRetrofitService(VideoService.clreplaced).getPopularVideoList(id, strategy, start, 10);
return observable;
}
@Override
public Observable<VideoListInfo> getPlayListVideoList(int id, int start) {
Observable<VideoListInfo> observable = mRepositoryManager.obtainRetrofitService(VideoService.clreplaced).getPlayListVideoList(id, start, 10);
return observable;
}
}
19
Source : VideoDetailModel.java
with Apache License 2.0
from Zweihui
with Apache License 2.0
from Zweihui
@ActivityScope
public clreplaced VideoDetailModel extends BaseModel implements VideoDetailContract.Model {
private Gson mGson;
private Application mApplication;
@Inject
public VideoDetailModel(IRepositoryManager repositoryManager, Gson gson, Application application) {
super(repositoryManager);
this.mGson = gson;
this.mApplication = application;
}
@Override
public void onDestroy() {
super.onDestroy();
this.mGson = null;
this.mApplication = null;
}
@Override
public Observable<VideoListInfo> getRelateVideoInfo(int id) {
Observable<VideoListInfo> observable = mRepositoryManager.obtainRetrofitService(VideoDetailService.clreplaced).getRelateVideoInfo(id);
return observable;
}
@Override
public Observable<VideoListInfo> getSecondRelateVideoInfo(String path, int id, int startCount) {
Observable<VideoListInfo> observable = mRepositoryManager.obtainRetrofitService(VideoDetailService.clreplaced).getSecondRelateVideoInfo(path, id, startCount, 10);
return observable;
}
@Override
public Observable<ReplyInfo> getAllReplyInfo(int videoId) {
Observable<ReplyInfo> observable = mRepositoryManager.obtainRetrofitService(VideoDetailService.clreplaced).getAllReplyInfo(videoId);
return observable;
}
@Override
public Observable<ReplyInfo> getMoreReplyInfo(int lastId, int videoId) {
Observable<ReplyInfo> observable = mRepositoryManager.obtainRetrofitService(VideoDetailService.clreplaced).getMoreReplyInfo(lastId, videoId, "video");
return observable;
}
@Override
public Observable<ShareInfo> getShareInfo(int idenreplacedy) {
Observable<ShareInfo> observable = mRepositoryManager.obtainRetrofitService(VideoDetailService.clreplaced).getShareInfo("VIDEO", "WEB_PAGE", idenreplacedy);
return observable;
}
@Override
public Observable<VideoListInfo.Video.VideoData> getVideoData(int id) {
Observable<VideoListInfo.Video.VideoData> observable = mRepositoryManager.obtainRetrofitService(VideoDetailService.clreplaced).getVideoData(id);
return observable;
}
}
19
Source : SplashModel.java
with Apache License 2.0
from Zweihui
with Apache License 2.0
from Zweihui
@ActivityScope
public clreplaced SplashModel extends BaseModel implements SplashContract.Model {
private Gson mGson;
private Application mApplication;
@Inject
public SplashModel(IRepositoryManager repositoryManager, Gson gson, Application application) {
super(repositoryManager);
this.mGson = gson;
this.mApplication = application;
}
@Override
public void onDestroy() {
super.onDestroy();
this.mGson = null;
this.mApplication = null;
}
@Override
public Observable<List<Category>> getCategories() {
Observable<List<Category>> categories = mRepositoryManager.obtainRetrofitService(SplashService.clreplaced).getCategories();
return mRepositoryManager.obtainCacheService(CommonCache.clreplaced).getCategories(categories).flatMap(new Function<Reply<List<Category>>, ObservableSource<List<Category>>>() {
@Override
public ObservableSource<List<Category>> apply(@NonNull Reply<List<Category>> listReply) throws Exception {
return Observable.just(listReply.getData());
}
});
}
}
19
Source : SearchModel.java
with Apache License 2.0
from Zweihui
with Apache License 2.0
from Zweihui
@ActivityScope
public clreplaced SearchModel extends BaseModel implements SearchContract.Model {
private Gson mGson;
private Application mApplication;
@Inject
public SearchModel(IRepositoryManager repositoryManager, Gson gson, Application application) {
super(repositoryManager);
this.mGson = gson;
this.mApplication = application;
}
@Override
public void onDestroy() {
super.onDestroy();
this.mGson = null;
this.mApplication = null;
}
@Override
public Observable<List<String>> getHotWord() {
Observable<List<String>> strs = mRepositoryManager.obtainRetrofitService(SearchService.clreplaced).getHotWord();
// 使用rxcache缓存,上拉刷新则不读取缓存,加载更多读取缓存
return mRepositoryManager.obtainCacheService(CommonCache.clreplaced).getHotWords(strs).flatMap(new Function<Reply<List<String>>, ObservableSource<List<String>>>() {
@Override
public ObservableSource<List<String>> apply(@NonNull Reply<List<String>> listReply) throws Exception {
return Observable.just(listReply.getData());
}
});
}
@Override
public Observable<VideoListInfo> getSearchList(String start, String query) {
Observable<VideoListInfo> observable = mRepositoryManager.obtainRetrofitService(SearchService.clreplaced).getSearchList(start, query);
return observable;
}
}
19
Source : HotModel.java
with Apache License 2.0
from Zweihui
with Apache License 2.0
from Zweihui
@ActivityScope
public clreplaced HotModel extends BaseModel implements HotContract.Model {
private Gson mGson;
private Application mApplication;
@Inject
public HotModel(IRepositoryManager repositoryManager, Gson gson, Application application) {
super(repositoryManager);
this.mGson = gson;
this.mApplication = application;
}
@Override
public void onDestroy() {
super.onDestroy();
this.mGson = null;
this.mApplication = null;
}
@Override
public Observable<VideoListInfo> getRankVideoList(String Stra) {
Observable<VideoListInfo> observable = mRepositoryManager.obtainRetrofitService(VideoService.clreplaced).getRankVideoList(Stra);
return mRepositoryManager.obtainCacheService(CommonCache.clreplaced).getRankVideoList(observable, new DynamicKey(Stra), new EvictDynamicKey(true)).flatMap(new Function<Reply<VideoListInfo>, ObservableSource<VideoListInfo>>() {
@Override
public ObservableSource<VideoListInfo> apply(@NonNull Reply<VideoListInfo> listReply) throws Exception {
return Observable.just(listReply.getData());
}
});
}
}
19
Source : HistoryModel.java
with Apache License 2.0
from Zweihui
with Apache License 2.0
from Zweihui
@ActivityScope
public clreplaced HistoryModel extends BaseModel implements HistoryContract.Model {
private Gson mGson;
private Application mApplication;
@Inject
public HistoryModel(IRepositoryManager repositoryManager, Gson gson, Application application) {
super(repositoryManager);
this.mGson = gson;
this.mApplication = application;
}
@Override
public void onDestroy() {
super.onDestroy();
this.mGson = null;
this.mApplication = null;
}
@Override
public Observable<List<VideoDaoEnreplacedy>> getListFromDb(int start) {
return Observable.create((ObservableOnSubscribe<List<VideoDaoEnreplacedy>>) e -> {
VideoDaoEnreplacedy enreplacedy = new VideoDaoEnreplacedy();
List<VideoDaoEnreplacedy> list = GreenDaoHelper.getInstance().create(enreplacedy.getDbName()).getMaster().newSession().getVideoDaoEnreplacedyDao().queryBuilder().limit(10).offset(start).orderDesc(VideoDaoEnreplacedyDao.Properties.Date).list();
List<VideoDaoEnreplacedy> infolist = new ArrayList<VideoDaoEnreplacedy>();
if (!StringUtils.isEmpty(list)) {
for (VideoDaoEnreplacedy enreplacedy1 : list) {
enreplacedy1.setVideo(mGson.fromJson(enreplacedy1.getBody(), VideoListInfo.Video.VideoData.clreplaced));
infolist.add(enreplacedy1);
}
}
e.onNext(infolist);
});
}
@Override
public Observable<Boolean> deleteFromDb(VideoDaoEnreplacedy daoEnreplacedy) {
return Observable.create((ObservableOnSubscribe<Boolean>) e -> {
DaoMaster master = GreenDaoHelper.getInstance().create(daoEnreplacedy.getDbName()).getMaster();
if (master.newSession().getVideoDaoEnreplacedyDao().loadByRowId(daoEnreplacedy.getId()) == null) {
} else {
master.newSession().getVideoDaoEnreplacedyDao().delete(daoEnreplacedy);
e.onNext(true);
}
});
}
@Override
public Observable<List<VideoDaoEnreplacedy>> getListFromNet(int start, String userid) {
return Observable.create((ObservableOnSubscribe<List<VideoDaoEnreplacedy>>) emitter -> {
BmobQuery<VideoDaoEnreplacedy> query = new BmobQuery<VideoDaoEnreplacedy>();
query.addWhereEqualTo("userId", userid);
query.setLimit(10);
query.order("-updatedAt");
query.setSkip(start);
query.findObjects(new FindListener<VideoDaoEnreplacedy>() {
@Override
public void done(List<VideoDaoEnreplacedy> list, BmobException e) {
List<VideoDaoEnreplacedy> infolist = new ArrayList<VideoDaoEnreplacedy>();
if (!StringUtils.isEmpty(list)) {
for (VideoDaoEnreplacedy enreplacedy1 : list) {
enreplacedy1.setVideo(mGson.fromJson(enreplacedy1.getBody(), VideoListInfo.Video.VideoData.clreplaced));
infolist.add(enreplacedy1);
}
}
emitter.onNext(infolist);
}
});
});
}
@Override
public Observable<Boolean> deleteFromNet(VideoDaoEnreplacedy enreplacedy) {
return Observable.create((ObservableOnSubscribe<Boolean>) emitter -> {
enreplacedy.delete(new UpdateListener() {
@Override
public void done(BmobException e) {
if (e == null) {
emitter.onNext(true);
}
}
});
});
}
}
19
Source : CategoryModel.java
with Apache License 2.0
from Zweihui
with Apache License 2.0
from Zweihui
@ActivityScope
public clreplaced CategoryModel extends BaseModel implements CategoryContract.Model {
private Gson mGson;
private Application mApplication;
@Inject
public CategoryModel(IRepositoryManager repositoryManager, Gson gson, Application application) {
super(repositoryManager);
this.mGson = gson;
this.mApplication = application;
}
@Override
public Observable<List<Category>> getCategories() {
Observable<List<Category>> categories = mRepositoryManager.obtainRetrofitService(SplashService.clreplaced).getCategories();
return mRepositoryManager.obtainCacheService(CommonCache.clreplaced).getCategories(categories).flatMap(new Function<Reply<List<Category>>, ObservableSource<List<Category>>>() {
@Override
public ObservableSource<List<Category>> apply(@NonNull Reply<List<Category>> listReply) throws Exception {
return Observable.just(listReply.getData());
}
});
}
@Override
public void onDestroy() {
super.onDestroy();
this.mGson = null;
this.mApplication = null;
}
}
19
Source : AuthorDetailModel.java
with Apache License 2.0
from Zweihui
with Apache License 2.0
from Zweihui
@ActivityScope
public clreplaced AuthorDetailModel extends BaseModel implements AuthorDetailContract.Model {
private Gson mGson;
private Application mApplication;
@Inject
public AuthorDetailModel(IRepositoryManager repositoryManager, Gson gson, Application application) {
super(repositoryManager);
this.mGson = gson;
this.mApplication = application;
}
@Override
public Observable<VideoListInfo> getAuthorVideoList(int id, int start) {
Observable<VideoListInfo> observable = mRepositoryManager.obtainRetrofitService(AuthorDetailService.clreplaced).getAuthorVideoList(id, start, 10);
return observable;
}
@Override
public Observable<AuthorTabsInfo> getAuthorTabs(int id) {
Observable<AuthorTabsInfo> observable = mRepositoryManager.obtainRetrofitService(AuthorDetailService.clreplaced).getAuthorTabs(id, "PGC");
return observable;
}
@Override
public Observable<AuthorIndexInfo> getAuthorIndexInfo(int id) {
Observable<AuthorIndexInfo> observable = mRepositoryManager.obtainRetrofitService(AuthorDetailService.clreplaced).getAuthorIndexInfo(id, "PGC");
return observable;
}
@Override
public Observable<AuthorDynamicInfo> getAuthorDynamicList(int id, int startCount) {
Observable<AuthorDynamicInfo> observable = mRepositoryManager.obtainRetrofitService(AuthorDetailService.clreplaced).getAuthorDynamicList(id, startCount, 10, "PGC");
return observable;
}
@Override
public Observable<AuthorAlbumInfo> getAuthorAlbumList(int id, int startCount) {
Observable<AuthorAlbumInfo> observable = mRepositoryManager.obtainRetrofitService(AuthorDetailService.clreplaced).getAuthorAlbumList(id, startCount, 10);
return observable;
}
@Override
public Observable<ShareInfo> getShareInfo(int idenreplacedy) {
Observable<ShareInfo> observable = mRepositoryManager.obtainRetrofitService(VideoDetailService.clreplaced).getShareInfo("PGC", "WEB_PAGE", idenreplacedy);
return observable;
}
@Override
public void onDestroy() {
super.onDestroy();
this.mGson = null;
this.mApplication = null;
}
}
19
Source : AttentionModel.java
with Apache License 2.0
from Zweihui
with Apache License 2.0
from Zweihui
@ActivityScope
public clreplaced AttentionModel extends BaseModel implements AttentionContract.Model {
private Gson mGson;
private Application mApplication;
@Inject
public AttentionModel(IRepositoryManager repositoryManager, Gson gson, Application application) {
super(repositoryManager);
this.mGson = gson;
this.mApplication = application;
}
@Override
public Observable<AttentionInfo> getAttentionVideoList(int start) {
Observable<AttentionInfo> observable = mRepositoryManager.obtainRetrofitService(AttentionService.clreplaced).getAttentionVideoList(start, 2);
return observable;
}
@Override
public Observable<List<MyAttentionEnreplacedy>> getMyAttentionList(String userid) {
return Observable.create((ObservableOnSubscribe<List<MyAttentionEnreplacedy>>) emitter -> {
BmobQuery<MyAttentionEnreplacedy> query = new BmobQuery<MyAttentionEnreplacedy>();
query.addWhereEqualTo("userId", userid);
query.order("-createdAt");
query.findObjects(new FindListener<MyAttentionEnreplacedy>() {
@Override
public void done(List<MyAttentionEnreplacedy> list, BmobException e) {
if (list != null)
emitter.onNext(list);
}
});
});
}
@Override
public void onDestroy() {
super.onDestroy();
this.mGson = null;
this.mApplication = null;
}
}
19
Source : BackstackDelegate.java
with Apache License 2.0
from Zhuinden
with Apache License 2.0
from Zhuinden
/**
* Convenience method that automatically handles calling the following methods:
* - {@link BackstackDelegate#onPostResume()}
* - {@link BackstackDelegate#onPause()}
* - {@link BackstackDelegate#onSaveInstanceState(Bundle)}
* - {@link BackstackDelegate#onDestroy()}.
*
* This method can only be called after {@link BackstackDelegate#onCreate(Bundle, Object, List)}.
*
* Note: This method cannot handle {@link BackstackDelegate#onRetainCustomNonConfigurationInstance()}, so that must still be called manually.
*
* @param activity the Activity whose callbacks we register for.
*/
@TargetApi(14)
public void registerForLifecycleCallbacks(@Nonnull final Activity activity) {
if (activity == null) {
throw new NullPointerException("Activity is null");
}
@SuppressWarnings("unused")
Backstack backstack = getBackstack();
final Application application = activity.getApplication();
application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity a, Bundle savedInstanceState) {
// this is executed too late. do nothing.
}
@Override
public void onActivityStarted(Activity a) {
if (activity == a) {
// do nothing
}
}
@Override
public void onActivityResumed(Activity a) {
if (activity == a) {
onPostResume();
}
}
@Override
public void onActivityPaused(Activity a) {
if (activity == a) {
onPause();
}
}
@Override
public void onActivityStopped(Activity a) {
if (activity == a) {
// do nothing
}
}
@Override
public void onActivitySaveInstanceState(Activity a, Bundle outState) {
if (activity == a) {
onSaveInstanceState(outState);
}
}
@Override
public void onActivityDestroyed(Activity a) {
if (activity == a) {
onDestroy();
application.unregisterActivityLifecycleCallbacks(this);
}
}
});
}
19
Source : EasyHttp.java
with Apache License 2.0
from zhou-you
with Apache License 2.0
from zhou-you
/**
* 必须在全局Application先调用,获取context上下文,否则缓存无法使用
*/
public static void init(Application app) {
sContext = app;
}
19
Source : MApplication.java
with Apache License 2.0
from zhou-you
with Apache License 2.0
from zhou-you
public clreplaced MApplication extends Application {
private static Application app = null;
@Override
public void onCreate() {
super.onCreate();
app = this;
EasyHttp.init(this);
// 这里涉及到安全我把url去掉了,demo都是调试通的
String Url = "http://www.xxx.com";
// 设置请求头
HttpHeaders headers = new HttpHeaders();
headers.put("User-Agent", SystemInfoUtils.getUserAgent(this, AppConstant.APPID));
// 设置请求参数
HttpParams params = new HttpParams();
params.put("appId", AppConstant.APPID);
EasyHttp.getInstance().debug("RxEasyHttp", BuildConfig.DEBUG).setReadTimeOut(60 * 1000).setWriteTimeOut(60 * 1000).setConnectTimeout(60 * 1000).setRetryCount(// 默认网络不好自动重试3次
3).setRetryDelay(// 每次延时500ms重试
500).setRetryIncreaseDelay(// 每次延时叠加500ms
500).setBaseUrl(Url).setCacheDiskConverter(// 默认缓存使用序列化转化
new SerializableDiskConverter()).setCacheMaxSize(// 设置缓存大小为50M
50 * 1024 * 1024).setCacheVersion(// 缓存版本为1
1).setHostnameVerifier(// 全局访问规则
new UnSafeHostnameVerifier(Url)).setCertificates().addCommonHeaders(// 设置全局公共头
headers).addCommonParams(// 设置全局公共参数
params).addInterceptor(// 添加参数签名拦截器
new CustomSignInterceptor());
// .addInterceptor(new HeTInterceptor());//处理自己业务的拦截器
}
public clreplaced UnSafeHostnameVerifier implements HostnameVerifier {
private String host;
public UnSafeHostnameVerifier(String host) {
this.host = host;
HttpLog.i("############### UnSafeHostnameVerifier " + host);
}
@Override
public boolean verify(String hostname, SSLSession session) {
HttpLog.i("############### verify " + hostname + " " + this.host);
if (this.host == null || "".equals(this.host) || !this.host.contains(hostname))
return false;
return true;
}
}
/**
* 获取Application的Context
*/
public static Context getAppContext() {
if (app == null)
return null;
return app.getApplicationContext();
}
}
19
Source : App.java
with Apache License 2.0
from zhonglikui
with Apache License 2.0
from zhonglikui
protected void init(boolean isDebug, Application application) {
this.mContext = application.getApplicationContext();
this.isDebug = isDebug;
}
19
Source : App.java
with Apache License 2.0
from zguop
with Apache License 2.0
from zguop
private void initLeakCanary(Application application) {
/*--------------- 内存泄漏的检测 ---------------*/
if (!BuildConfig.DEBUG || LeakCanary.isInreplacedyzerProcess(application)) {
return;
}
LeakCanary.install(application);
}
19
Source : OnCreatHooker.java
with Apache License 2.0
from Zane96
with Apache License 2.0
from Zane96
public static void hookOnCreat(Application context, final OnCreatListener listener) {
context.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
listener.beforeOnCreat(activity, savedInstanceState);
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityResumed(Activity activity) {
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
@Override
public void onActivityDestroyed(Activity activity) {
}
});
}
19
Source : Hooker.java
with Apache License 2.0
from Zane96
with Apache License 2.0
from Zane96
/**
* Hook startActivity/startActivityForResult两个方法
* Hook activity的onCreat()方法
*/
public static void hookRouter(Application context) {
hookStart();
OnCreatHooker.hookOnCreat(context, new OnCreatListener() {
@Override
public void beforeOnCreat(Activity activity, Bundle savedInstanceState) {
injectMan.injectParam(activity);
}
});
}
19
Source : EasyRouter.java
with Apache License 2.0
from Zane96
with Apache License 2.0
from Zane96
/**
* 全局hook
*/
public static void init(Application context, Converter.Factory factory) {
Hooker.hookRouter(context);
EasyRouterSet.setConverterFactory(factory);
}
19
Source : AppManager.java
with GNU General Public License v3.0
from z-chu
with GNU General Public License v3.0
from z-chu
/**
* 作者: 赵成柱 on 2016/7/14.
* app管理器,管理所有activity的生命周期
*/
public clreplaced AppManager {
private static final boolean isShowLog = BuildConfig.DEBUG;
private static final String TAG = "AppManager";
private Application mApplication;
@SuppressLint("StaticFieldLeak")
private static AppManager sInstance;
private Stack<Activity> mActivityStack;
// 前台activity数
private int mStageActivityCount = 0;
private AppManager(Application application) {
mActivityStack = new Stack<>();
mApplication = application;
application.registerActivityLifecycleCallbacks(new MyActivityLifecycleCallbacks());
}
public static AppManager init(Application application) {
if (application == null) {
throw new NullPointerException("You cannot start a init on a null Application");
}
if (sInstance == null) {
sInstance = new AppManager(application);
}
return sInstance;
}
public static AppManager getInstance() {
return sInstance;
}
/**
* 获取指定的Activity
*/
public Activity getActivity(Clreplaced<?> cls) {
if (mActivityStack != null)
for (Activity activity : mActivityStack) {
if (activity.getClreplaced().equals(cls)) {
return activity;
}
}
return null;
}
/**
* 添加Activity到堆栈
*/
private boolean addActivity(Activity activity) {
return activity != null && mActivityStack.add(activity);
}
private boolean removeActivity(Activity activity) {
return activity != null && mActivityStack.remove(activity);
}
/**
* 获取当前Activity(堆栈中最后一个压入的)
*/
public Activity currentActivity() {
Activity activity = null;
if (!mActivityStack.empty()) {
activity = mActivityStack.lastElement();
}
return activity;
}
public Activity beforeActivity() {
Activity activity = null;
if (mActivityStack.size() > 1) {
activity = mActivityStack.get(mActivityStack.size() - 2);
}
return activity;
}
public Activity beforeActivity(Activity activity) {
Activity beforeActivity = null;
int indexOf = mActivityStack.indexOf(activity);
if (indexOf >= 1) {
beforeActivity = mActivityStack.get(indexOf - 1);
}
return beforeActivity;
}
/**
* 结束当前Activity(堆栈中最后一个压入的)
*/
public void finishActivity() {
if (!mActivityStack.empty()) {
finishActivity(mActivityStack.pop());
}
}
/**
* 结束指定的Activity
*/
public void finishActivity(Activity activity) {
if (activity != null && !activity.isFinishing()) {
mActivityStack.remove(activity);
activity.finish();
}
}
/**
* 结束指定类名的所有Activity
*/
public void finishActivity(Clreplaced<?> cls) {
Iterator<Activity> iterator = mActivityStack.iterator();
while (iterator.hasNext()) {
Activity activity = iterator.next();
if (activity.getClreplaced().equals(cls)) {
iterator.remove();
activity.finish();
}
}
}
/**
* 结束指定非类名的所有Activity
*/
public void finishOtherActivity(Clreplaced<?> cls) {
Iterator<Activity> iterator = mActivityStack.iterator();
while (iterator.hasNext()) {
Activity activity = iterator.next();
if (!activity.getClreplaced().equals(cls)) {
iterator.remove();
activity.finish();
}
}
}
/**
* 结束所有Activity
*/
public void finishAllActivity() {
Iterator<Activity> iterator = mActivityStack.iterator();
while (iterator.hasNext()) {
Activity activity = iterator.next();
iterator.remove();
activity.finish();
}
}
public Object[] getActivityArray() {
return mActivityStack.toArray();
}
/**
* 判断应用是否处于前台
*/
public boolean isStagApp() {
return mStageActivityCount > 0;
}
/**
* 重启应用程序
*/
public void resetApp() {
Intent i = mApplication.getPackageManager().getLaunchIntentForPackage(mApplication.getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mApplication.startActivity(i);
exit();
}
public void exit() {
try {
finishAllActivity();
// 友盟统计,统计关闭
MobclickAgent.onKillProcess(mApplication);
// 杀死该应用进程
android.os.Process.killProcess(android.os.Process.myPid());
} catch (Exception e) {
Logger.e(e);
}
}
private clreplaced MyActivityLifecycleCallbacks implements Application.ActivityLifecycleCallbacks {
@Override
public void onActivityCreated(Activity activity, Bundle bundle) {
if (isShowLog) {
Log.i(TAG, "onActivityCreated :" + activity);
}
AppManager.sInstance.addActivity(activity);
}
@Override
public void onActivityStarted(Activity activity) {
if (isShowLog) {
Log.i(TAG, "onActivityStarted :" + activity);
}
mStageActivityCount++;
}
@Override
public void onActivityResumed(Activity activity) {
if (isShowLog) {
Log.i(TAG, "onActivityResumed :" + activity);
}
}
@Override
public void onActivityPaused(Activity activity) {
if (isShowLog) {
Log.i(TAG, "onActivityPaused :" + activity);
}
}
@Override
public void onActivityStopped(Activity activity) {
if (isShowLog) {
Log.i(TAG, "onActivityStopped :" + activity);
}
mStageActivityCount--;
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
if (isShowLog) {
Log.i(TAG, "onActivitySaveInstanceState :" + activity);
}
}
@Override
public void onActivityDestroyed(Activity activity) {
if (isShowLog) {
Log.i(TAG, "onActivityDestroyed :" + activity);
}
AppManager.sInstance.removeActivity(activity);
}
}
}
19
Source : AppManager.java
with GNU General Public License v3.0
from z-chu
with GNU General Public License v3.0
from z-chu
public static AppManager init(Application application) {
if (application == null) {
throw new NullPointerException("You cannot start a init on a null Application");
}
if (sInstance == null) {
sInstance = new AppManager(application);
}
return sInstance;
}
19
Source : CrashHelper.java
with GNU General Public License v3.0
from ywwynm
with GNU General Public License v3.0
from ywwynm
/**
* Created by ywwynm on 2016/4/29.
* helper for crash that caused by uncaught exceptions.
*/
public clreplaced CrashHelper implements Thread.UncaughtExceptionHandler {
public static final String TAG = "CrashHelper";
private static CrashHelper sCrashHelper;
private Application mApplication;
private Thread.UncaughtExceptionHandler mDefaultHandler;
public static CrashHelper getInstance() {
if (sCrashHelper == null) {
synchronized (CrashHelper.clreplaced) {
if (sCrashHelper == null) {
sCrashHelper = new CrashHelper();
}
}
}
return sCrashHelper;
}
private CrashHelper() {
}
public void init(Application application) {
mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(this);
mApplication = application;
}
@Override
public void uncaughtException(Thread thread, Throwable ex) {
saveCrashInfoToStorage(ex);
createFileToShowFeedbackDialogNextLaunch();
ex.printStackTrace();
if (mDefaultHandler != null) {
mDefaultHandler.uncaughtException(thread, ex);
} else {
Process.killProcess(Process.myPid());
}
}
private void saveCrashInfoToStorage(Throwable ex) {
String path = Def.Meta.APP_FILE_DIR + "/log";
String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
String name = "crash_" + time + ".log";
File file = FileUtil.createFile(path, name);
if (file == null) {
return;
}
try {
PrintWriter writer = new PrintWriter(new FileWriter(file));
writer.println(time);
writer.print("APP Version: ");
writer.println(BuildConfig.VERSION_NAME + "_" + BuildConfig.VERSION_CODE);
writer.println(DeviceUtil.getDeviceInfo());
writer.println();
ex.printStackTrace(writer);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void createFileToShowFeedbackDialogNextLaunch() {
try {
FileOutputStream fos = mApplication.openFileOutput(Def.Meta.FEEDBACK_ERROR_FILE_NAME, Context.MODE_PRIVATE);
fos.write(App.getApp().getString(R.string.qq_my_love).getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
19
Source : CrashHelper.java
with GNU General Public License v3.0
from ywwynm
with GNU General Public License v3.0
from ywwynm
public void init(Application application) {
mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(this);
mApplication = application;
}
19
Source : DelegateApplication.java
with Apache License 2.0
from YuntaoWei
with Apache License 2.0
from YuntaoWei
/**
* Created by oceanzhang on 16/10/8.
*/
public clreplaced DelegateApplication extends Application {
private Application app;
public DelegateApplication(Application app) {
this.app = app;
}
private static final String TAG = "DelegateApplication";
@Override
public Context getBaseContext() {
Log.d(TAG, "----getBaseContext");
return app.getBaseContext();
}
@Override
public replacedetManager getreplacedets() {
return app.getreplacedets();
}
@Override
public Resources getResources() {
return app.getResources();
}
@Override
public PackageManager getPackageManager() {
Log.d(TAG, "----getPackageManager");
return app.getPackageManager();
}
@Override
public ContentResolver getContentResolver() {
return app.getContentResolver();
}
@Override
public Looper getMainLooper() {
return app.getMainLooper();
}
@Override
public Context getApplicationContext() {
Log.d(TAG, "----getApplicationContext");
return app.getApplicationContext();
}
@Override
public void setTheme(int resid) {
app.setTheme(resid);
}
@Override
public Resources.Theme getTheme() {
return app.getTheme();
}
@Override
public ClreplacedLoader getClreplacedLoader() {
Log.d(TAG, "----getBaseContext");
return app.getClreplacedLoader();
}
@Override
public String getPackageName() {
Log.d(TAG, "----getPackageName");
return "com.xunlei.downloadprovider";
}
@Override
public ApplicationInfo getApplicationInfo() {
Log.d(TAG, "----getApplicationInfo");
return app.getApplicationInfo();
}
@Override
public String getPackageResourcePath() {
Log.d(TAG, "----getBaseContext");
return app.getPackageResourcePath();
}
@Override
public String getPackageCodePath() {
Log.d(TAG, "----getBaseContext");
return app.getPackageCodePath();
}
@Override
public SharedPreferences getSharedPreferences(String name, int mode) {
Log.d(TAG, "----getBaseContext");
return app.getSharedPreferences(name, mode);
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public boolean moveSharedPreferencesFrom(Context sourceContext, String name) {
Log.d(TAG, "----getBaseContext");
return app.moveSharedPreferencesFrom(sourceContext, name);
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public boolean deleteSharedPreferences(String name) {
Log.d(TAG, "----getBaseContext");
return app.deleteSharedPreferences(name);
}
@Override
public FileInputStream openFileInput(String name) throws FileNotFoundException {
Log.d(TAG, "----getBaseContext");
return app.openFileInput(name);
}
@Override
public FileOutputStream openFileOutput(String name, int mode) throws FileNotFoundException {
Log.d(TAG, "----getBaseContext");
return app.openFileOutput(name, mode);
}
@Override
public boolean deleteFile(String name) {
Log.d(TAG, "----getBaseContext");
return app.deleteFile(name);
}
@Override
public File getFileStreamPath(String name) {
Log.d(TAG, "----getBaseContext");
return app.getFileStreamPath(name);
}
@Override
public String[] fileList() {
return app.fileList();
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public File getDataDir() {
return app.getDataDir();
}
@Override
public File getFilesDir() {
return app.getFilesDir();
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public File getNoBackupFilesDir() {
return app.getNoBackupFilesDir();
}
@Override
public File getExternalFilesDir(String type) {
return app.getExternalFilesDir(type);
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public File[] getExternalFilesDirs(String type) {
return app.getExternalFilesDirs(type);
}
@Override
public File getObbDir() {
return app.getObbDir();
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public File[] getObbDirs() {
return app.getObbDirs();
}
@Override
public File getCacheDir() {
return app.getCacheDir();
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public File getCodeCacheDir() {
return app.getCodeCacheDir();
}
@Override
public File getExternalCacheDir() {
return app.getExternalCacheDir();
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public File[] getExternalCacheDirs() {
return app.getExternalCacheDirs();
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public File[] getExternalMediaDirs() {
return app.getExternalMediaDirs();
}
@Override
public File getDir(String name, int mode) {
return app.getDir(name, mode);
}
@Override
public SQLiteDatabase openOrCreateDatabase(String name, int mode, SQLiteDatabase.CursorFactory factory) {
return app.openOrCreateDatabase(name, mode, factory);
}
@Override
public SQLiteDatabase openOrCreateDatabase(String name, int mode, SQLiteDatabase.CursorFactory factory, DatabaseErrorHandler errorHandler) {
return app.openOrCreateDatabase(name, mode, factory, errorHandler);
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public boolean moveDatabaseFrom(Context sourceContext, String name) {
return app.moveDatabaseFrom(sourceContext, name);
}
@Override
public boolean deleteDatabase(String name) {
return app.deleteDatabase(name);
}
@Override
public File getDatabasePath(String name) {
return app.getDatabasePath(name);
}
@Override
public String[] databaseList() {
return app.databaseList();
}
@Override
public Drawable getWallpaper() {
return app.getWallpaper();
}
@Override
public Drawable peekWallpaper() {
return app.peekWallpaper();
}
@Override
public int getWallpaperDesiredMinimumWidth() {
return app.getWallpaperDesiredMinimumWidth();
}
@Override
public int getWallpaperDesiredMinimumHeight() {
return app.getWallpaperDesiredMinimumHeight();
}
@Override
public void setWallpaper(Bitmap bitmap) throws IOException {
app.setWallpaper(bitmap);
}
@Override
public void setWallpaper(InputStream data) throws IOException {
app.setWallpaper(data);
}
@Override
public void clearWallpaper() throws IOException {
app.clearWallpaper();
}
@Override
public void startActivity(Intent intent) {
app.startActivity(intent);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void startActivity(Intent intent, Bundle options) {
app.startActivity(intent, options);
}
@Override
public void startActivities(Intent[] intents) {
app.startActivities(intents);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void startActivities(Intent[] intents, Bundle options) {
app.startActivities(intents, options);
}
@Override
public void startIntentSender(IntentSender intent, Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) throws IntentSender.SendIntentException {
app.startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void startIntentSender(IntentSender intent, Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) throws IntentSender.SendIntentException {
app.startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags, options);
}
@Override
public void sendBroadcast(Intent intent) {
app.sendBroadcast(intent);
}
@Override
public void sendBroadcast(Intent intent, String receiverPermission) {
app.sendBroadcast(intent, receiverPermission);
}
@Override
public void sendOrderedBroadcast(Intent intent, String receiverPermission) {
app.sendOrderedBroadcast(intent, receiverPermission);
}
@Override
public void sendOrderedBroadcast(Intent intent, String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
app.sendOrderedBroadcast(intent, receiverPermission, resultReceiver, scheduler, initialCode, initialData, initialExtras);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public void sendBroadcastAsUser(Intent intent, UserHandle user) {
app.sendBroadcastAsUser(intent, user);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public void sendBroadcastAsUser(Intent intent, UserHandle user, String receiverPermission) {
app.sendBroadcastAsUser(intent, user, receiverPermission);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user, String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
app.sendOrderedBroadcastAsUser(intent, user, receiverPermission, resultReceiver, scheduler, initialCode, initialData, initialExtras);
}
@Override
public void sendStickyBroadcast(Intent intent) {
app.sendStickyBroadcast(intent);
}
@Override
public void sendStickyOrderedBroadcast(Intent intent, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
app.sendStickyOrderedBroadcast(intent, resultReceiver, scheduler, initialCode, initialData, initialExtras);
}
@Override
public void removeStickyBroadcast(Intent intent) {
app.removeStickyBroadcast(intent);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) {
app.sendStickyBroadcastAsUser(intent, user);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public void sendStickyOrderedBroadcastAsUser(Intent intent, UserHandle user, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
app.sendStickyOrderedBroadcastAsUser(intent, user, resultReceiver, scheduler, initialCode, initialData, initialExtras);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) {
app.removeStickyBroadcastAsUser(intent, user);
}
@Override
public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
return app.registerReceiver(receiver, filter);
}
@Override
public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, String broadcastPermission, Handler scheduler) {
return app.registerReceiver(receiver, filter, broadcastPermission, scheduler);
}
@Override
public void unregisterReceiver(BroadcastReceiver receiver) {
app.unregisterReceiver(receiver);
}
@Override
public ComponentName startService(Intent service) {
return app.startService(service);
}
@Override
public boolean stopService(Intent name) {
return app.stopService(name);
}
@Override
public boolean bindService(Intent service, ServiceConnection conn, int flags) {
return app.bindService(service, conn, flags);
}
@Override
public void unbindService(ServiceConnection conn) {
app.unbindService(conn);
}
@Override
public boolean startInstrumentation(ComponentName clreplacedName, String profileFile, Bundle arguments) {
return app.startInstrumentation(clreplacedName, profileFile, arguments);
}
@Override
public Object getSystemService(String name) {
return app.getSystemService(name);
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public String getSystemServiceName(Clreplaced<?> serviceClreplaced) {
return app.getSystemServiceName(serviceClreplaced);
}
@Override
public int checkPermission(String permission, int pid, int uid) {
return app.checkPermission(permission, pid, uid);
}
@Override
public int checkCallingPermission(String permission) {
return app.checkCallingPermission(permission);
}
@Override
public int checkCallingOrSelfPermission(String permission) {
return app.checkCallingOrSelfPermission(permission);
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public int checkSelfPermission(String permission) {
return app.checkSelfPermission(permission);
}
@Override
public void enforcePermission(String permission, int pid, int uid, String message) {
app.enforcePermission(permission, pid, uid, message);
}
@Override
public void enforceCallingPermission(String permission, String message) {
app.enforceCallingPermission(permission, message);
}
@Override
public void enforceCallingOrSelfPermission(String permission, String message) {
app.enforceCallingOrSelfPermission(permission, message);
}
@Override
public void grantUriPermission(String toPackage, Uri uri, int modeFlags) {
app.grantUriPermission(toPackage, uri, modeFlags);
}
@Override
public void revokeUriPermission(Uri uri, int modeFlags) {
app.revokeUriPermission(uri, modeFlags);
}
@Override
public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
return app.checkUriPermission(uri, pid, uid, modeFlags);
}
@Override
public int checkCallingUriPermission(Uri uri, int modeFlags) {
return app.checkCallingUriPermission(uri, modeFlags);
}
@Override
public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) {
return app.checkCallingOrSelfUriPermission(uri, modeFlags);
}
@Override
public int checkUriPermission(Uri uri, String readPermission, String writePermission, int pid, int uid, int modeFlags) {
return app.checkUriPermission(uri, readPermission, writePermission, pid, uid, modeFlags);
}
@Override
public void enforceUriPermission(Uri uri, int pid, int uid, int modeFlags, String message) {
app.enforceUriPermission(uri, pid, uid, modeFlags, message);
}
@Override
public void enforceCallingUriPermission(Uri uri, int modeFlags, String message) {
app.enforceCallingUriPermission(uri, modeFlags, message);
}
@Override
public void enforceCallingOrSelfUriPermission(Uri uri, int modeFlags, String message) {
app.enforceCallingOrSelfUriPermission(uri, modeFlags, message);
}
@Override
public void enforceUriPermission(Uri uri, String readPermission, String writePermission, int pid, int uid, int modeFlags, String message) {
app.enforceUriPermission(uri, readPermission, writePermission, pid, uid, modeFlags, message);
}
@Override
public Context createPackageContext(String packageName, int flags) throws PackageManager.NameNotFoundException {
return app.createPackageContext(packageName, flags);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public Context createConfigurationContext(Configuration overrideConfiguration) {
return app.createConfigurationContext(overrideConfiguration);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public Context createDisplayContext(Display display) {
return app.createDisplayContext(display);
}
@Override
public boolean isRestricted() {
return app.isRestricted();
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public Context createDeviceProtectedStorageContext() {
return app.createDeviceProtectedStorageContext();
}
public DelegateApplication() {
super();
}
@Override
public void onCreate() {
app.onCreate();
}
@Override
public void onTerminate() {
app.onTerminate();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
app.onConfigurationChanged(newConfig);
}
@Override
public void onLowMemory() {
app.onLowMemory();
}
@Override
public void onTrimMemory(int level) {
app.onTrimMemory(level);
}
@Override
public void registerComponentCallbacks(ComponentCallbacks callback) {
app.registerComponentCallbacks(callback);
}
@Override
public void unregisterComponentCallbacks(ComponentCallbacks callback) {
app.unregisterComponentCallbacks(callback);
}
@Override
public void registerActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback) {
app.registerActivityLifecycleCallbacks(callback);
}
@Override
public void unregisterActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback) {
app.unregisterActivityLifecycleCallbacks(callback);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public void registerOnProvidereplacedistDataListener(OnProvidereplacedistDataListener callback) {
app.registerOnProvidereplacedistDataListener(callback);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public void unregisterOnProvidereplacedistDataListener(OnProvidereplacedistDataListener callback) {
app.unregisterOnProvidereplacedistDataListener(callback);
}
@Override
public int hashCode() {
return app.hashCode();
}
@Override
public boolean equals(Object obj) {
return app.equals(obj);
}
@Override
public String toString() {
return app.toString();
}
}
19
Source : DMLogger.java
with Apache License 2.0
from yujintao529
with Apache License 2.0
from yujintao529
public static void initDefaultLogger(Application application) {
LoggerManager.putAppender(LogcatAppender.defaultLogcatAppender());
FileAppender.Builder builder = new FileAppender.Builder();
LoggerManager.putAppender(builder.withFilePath(application.getFilesDir().getAbsolutePath() + "/logs").build());
LoggerManager.LoggerBuilder loggerBuilder = LoggerManager.newLoggerBuilder();
loggerBuilder.loggerName("DMLogger");
defaultLogger = loggerBuilder.build();
}
19
Source : Snake.java
with Apache License 2.0
from yuanhoujun
with Apache License 2.0
from yuanhoujun
/**
* 框架入口,用于集成滑动关闭功能
*
* @author Scott Smith 2017-12-11 12:17
*/
public clreplaced Snake {
private static Application mContext;
/**
* Initializes snake with default configurations, it will find configuration file in replacedet/snake.xml,
* Snake will use default configruation if not found snake.xml.
*
* @param application application instance
*/
public static void init(Application application) {
application.registerActivityLifecycleCallbacks(new GlobalActivityLifecycleDelegate() {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
ActivityManager.get().put(activity);
openDragToCloseForActivity(activity);
Logger.d(activity.getClreplaced() + " onCreate completed...");
}
@Override
public void onActivityDestroyed(Activity activity) {
ActivityManager.get().remove(activity);
Logger.d(activity.getClreplaced() + " destoryed completed...");
}
});
SnakeConfigReader.get().init(application);
mContext = application;
}
/**
* Create a fragment proxy object using the specified constructor.
* It will be use the empty parameter constructor if not specify the primaryconstructor annotation
* in constructor.
*
* @param fragment specified fragment clreplaced
* @param args specified constructor parameters
* @param <T> the child clreplacedes of android.app.fragment.
*
* @return fragment proxy object
*/
public static <T extends android.app.Fragment> T newProxy(Clreplaced<? extends T> fragment, Object... args) {
checkAnnotationNotEmpty(fragment);
try {
String clreplacedName = fragment.getName() + "_SnakeProxy";
if (com.youngfeng.snake.app.Fragment.clreplaced.isreplacedignableFrom(fragment)) {
clreplacedName = fragment.getName();
}
Clreplaced<?> snakeProxyClreplaced = Clreplaced.forName(clreplacedName);
Constructor<?>[] constructors = snakeProxyClreplaced.getConstructors();
Constructor<?> primaryConstructor = null;
for (Constructor<?> constructor : constructors) {
PrimaryConstructor primaryConstructorAnno = constructor.getAnnotation(PrimaryConstructor.clreplaced);
if (null != primaryConstructorAnno) {
primaryConstructor = constructor;
break;
}
}
T result = null;
if (null != primaryConstructor) {
result = (T) primaryConstructor.newInstance(args);
} else {
result = (T) snakeProxyClreplaced.newInstance();
}
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Create a support fragment proxy object using the specified constructor.
* It will be use the empty parameter constructor if not specify the primaryconstructor annotation
* in constructor.
*
* @param fragment specified support fragment clreplaced
* @param args specified constructor parameters
* @param <T> the child clreplacedes of android.support.v4.app.fragment.
*
* @return support fragment proxy object
*/
public static <T extends androidx.fragment.app.Fragment> T newProxySupport(Clreplaced<? extends T> fragment, Object... args) {
checkAnnotationNotEmpty(fragment);
try {
String clreplacedName = fragment.getName() + "_SnakeProxy";
if (com.youngfeng.snake.androidx.app.Fragment.clreplaced.isreplacedignableFrom(fragment)) {
clreplacedName = fragment.getName();
}
Clreplaced<?> snakeProxyClreplaced = Clreplaced.forName(clreplacedName);
Constructor<?>[] constructors = snakeProxyClreplaced.getConstructors();
Constructor<?> primaryConstructor = null;
for (Constructor<?> constructor : constructors) {
PrimaryConstructor primaryConstructorAnno = constructor.getAnnotation(PrimaryConstructor.clreplaced);
if (null != primaryConstructorAnno) {
primaryConstructor = constructor;
break;
}
}
T result = null;
if (null != primaryConstructor) {
result = (T) primaryConstructor.newInstance(args);
} else {
result = (T) snakeProxyClreplaced.newInstance();
}
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
// replacedet annotation EnableDragToClose not empty
private static void checkAnnotationNotEmpty(Clreplaced<?> clazz) {
if (clazz.getAnnotation(EnableDragToClose.clreplaced) == null) {
throw new IllegalStateException(String.format("Please add %s annotation to clreplaced %s first, eg: @%s.", EnableDragToClose.clreplaced.getName(), clazz.getName(), EnableDragToClose.clreplaced.getSimpleName()));
}
}
/**
* Open DragToClose for fragment, just for internal using.
*
* @param snakeHackLayout SnakeHackLayout
* @param fragment the current fragment
*/
public static void openDragToCloseForFragment(@NonNull SnakeHackLayout snakeHackLayout, @NonNull final android.app.Fragment fragment) {
replacedertFragmentActive(fragment);
setDragParameter(fragment.getClreplaced().getAnnotation(SetDragParameter.clreplaced), snakeHackLayout);
final FragmentManagerHelper fragmentManagerHelper = FragmentManagerHelper.get(fragment.getFragmentManager());
snakeHackLayout.setOnEdgeDragListener(new SnakeHackLayout.OnEdgeDragListener() {
private int mVisibility = -1;
@Override
public void onDragStart(SnakeHackLayout parent) {
SoftKeyboardHelper.hideKeyboard(fragment);
}
@SuppressLint("WrongConstant")
@Override
public void onDrag(SnakeHackLayout parent, View view, int left) {
View viewOfLastFragment = fragmentManagerHelper.getViewOfLastFragment();
if (null != viewOfLastFragment) {
if (View.VISIBLE != viewOfLastFragment.getVisibility()) {
mVisibility = viewOfLastFragment.getVisibility();
viewOfLastFragment.setVisibility(View.VISIBLE);
viewOfLastFragment.setX(0f);
}
if (parent.getUIConfig().allowPageLinkage) {
float ratio = (left * 1.0f) / parent.getWidth();
viewOfLastFragment.setX((ratio - 1) * Utils.dp2px(fragment.getActivity(), 100f));
}
}
}
@Override
public void onRelease(SnakeHackLayout parent, View view, int left, boolean shouldClose, int interceptScene) {
if (shouldClose) {
parent.smoothScrollToLeave(view, new SnakeHackLayout.OnReleaseStateListener() {
@Override
public void onReleaseCompleted(SnakeHackLayout parent, View view) {
View viewOfLastFragment = fragmentManagerHelper.getViewOfLastFragment();
if (null != viewOfLastFragment) {
viewOfLastFragment.setX(0f);
}
android.app.Fragment lastFragment = fragmentManagerHelper.getLastFragment();
disableAnimation(lastFragment, true);
if (fragmentManagerHelper.backToLastFragment()) {
disableAnimation(lastFragment, false);
} else {
disableAnimation(lastFragment, false);
}
}
});
} else {
parent.smoothScrollToStart(view, new SnakeHackLayout.OnReleaseStateListener() {
@SuppressLint("WrongConstant")
@Override
public void onReleaseCompleted(SnakeHackLayout parent, View view) {
View viewOfLastFragment = fragmentManagerHelper.getViewOfLastFragment();
if (null != viewOfLastFragment) {
if (mVisibility >= 0) {
viewOfLastFragment.setVisibility(mVisibility);
}
viewOfLastFragment.setX(0f);
}
}
});
}
}
});
}
/**
* Open DragToClose for fragment, just for internal using.
*
* @param snakeHackLayout SnakeHackLayout
* @param fragment the current fragment
*/
public static void openDragToCloseForFragment(@NonNull SnakeHackLayout snakeHackLayout, @NonNull final androidx.fragment.app.Fragment fragment) {
replacedertFragmentActive(fragment);
setDragParameter(fragment.getClreplaced().getAnnotation(SetDragParameter.clreplaced), snakeHackLayout);
final FragmentManagerHelper fragmentManagerHelper = FragmentManagerHelper.get(fragment.getFragmentManager());
snakeHackLayout.setOnEdgeDragListener(new SnakeHackLayout.OnEdgeDragListener() {
private androidx.fragment.app.Fragment lastFragment;
@Override
public void onDragStart(SnakeHackLayout parent) {
SoftKeyboardHelper.hideKeyboard(fragment);
}
@Override
public void onDrag(SnakeHackLayout parent, View view, int left) {
if (null == lastFragment)
lastFragment = fragmentManagerHelper.getLastAndroidXFragment(fragment);
if (null == lastFragment)
return;
if (lastFragment.isDetached() || lastFragment.isRemoving())
return;
View viewOfLastFragment = lastFragment.getView();
if (null != viewOfLastFragment) {
if (View.VISIBLE != viewOfLastFragment.getVisibility()) {
viewOfLastFragment.setVisibility(View.VISIBLE);
}
View contentView = viewOfLastFragment;
if (viewOfLastFragment instanceof SnakeHackLayout) {
contentView = ((SnakeHackLayout) viewOfLastFragment).getContentView();
}
if (parent.getUIConfig().allowPageLinkage) {
float ratio = (left * 1.0f) / parent.getWidth();
float offsetLeft = (ratio - 1) * Utils.dp2px(fragment.requireActivity(), 100f);
contentView.setLeft((int) offsetLeft);
}
}
}
@Override
public void onRelease(SnakeHackLayout parent, View view, int left, final boolean shouldClose, int interceptScene) {
if (shouldClose) {
disableAnimation(lastFragment, true);
parent.smoothScrollToLeave(view, new SnakeHackLayout.OnReleaseStateListener() {
@Override
public void onReleaseCompleted(SnakeHackLayout parent, View view) {
if (null == lastFragment)
lastFragment = fragmentManagerHelper.getLastAndroidXFragment(fragment);
if (null == lastFragment)
return;
View viewOfLastFragment = lastFragment.getView();
resetContentViewForFragment(viewOfLastFragment);
fragmentManagerHelper.backToAndroidXFragment();
disableAnimation(lastFragment, false);
}
});
} else {
parent.smoothScrollToStart(view, new SnakeHackLayout.OnReleaseStateListener() {
@SuppressLint("WrongConstant")
@Override
public void onReleaseCompleted(SnakeHackLayout parent, View view) {
if (null == lastFragment)
lastFragment = fragmentManagerHelper.getLastAndroidXFragment(fragment);
if (null == lastFragment)
return;
View viewOfLastFragment = lastFragment.getView();
if (null != viewOfLastFragment) {
resetContentViewForFragment(viewOfLastFragment);
}
}
});
}
}
});
}
private static void resetContentViewForFragment(View root) {
View contentView = root;
if (root instanceof SnakeHackLayout) {
contentView = ((SnakeHackLayout) root).getContentView();
}
if (null != contentView) {
contentView.setLeft(0);
if (root instanceof SnakeHackLayout) {
((SnakeHackLayout) root).resetUI();
}
}
}
private static void replacedertFragmentActive(android.app.Fragment fragment) {
if (fragment.isDetached() || fragment.isRemoving()) {
throw new IllegalStateException("You can't add this feature to a detached or removing fragment");
}
}
private static void replacedertFragmentActive(androidx.fragment.app.Fragment fragment) {
if (fragment.isDetached() || fragment.isRemoving()) {
throw new IllegalStateException("You can't add this feature to a detached or removing fragment");
}
}
private static void setDragParameter(@Nullable SetDragParameter dragParameter, SnakeHackLayout snakeHackLayout) {
if (null != dragParameter) {
snakeHackLayout.hideShadowOfEdge(dragParameter.hideShadowOfEdge());
snakeHackLayout.setMinVelocity(dragParameter.minVelocity());
snakeHackLayout.setOnlyListenToFastSwipe(dragParameter.onlyListenToFastSwipe());
snakeHackLayout.enableSwipeUpToHome(dragParameter.enableSwipeUpToHome());
snakeHackLayout.setAllowPageLinkageOfUIConfig(dragParameter.allowPageLinkage());
if (!dragParameter.hideShadowOfEdge()) {
try {
snakeHackLayout.setShadowStartColor(Color.parseColor(dragParameter.shadowStartColor()));
} catch (IllegalArgumentException e) {
throw new SnakeConfigException(String.format("The shadow start color string of %s annotation is set error, eg: #ff0000, current value: %s", SetDragParameter.clreplaced.getSimpleName(), dragParameter.shadowStartColor()));
}
try {
snakeHackLayout.setShadowEndColor(Color.parseColor(dragParameter.shadowEndColor()));
} catch (IllegalArgumentException e) {
throw new SnakeConfigException(String.format("The shadow end color string of %s annotation is set error, eg: #ff0000, current value: %s", SetDragParameter.clreplaced.getSimpleName(), dragParameter.shadowEndColor()));
}
}
} else {
snakeHackLayout.hideShadowOfEdge(SnakeConfigReader.get().hideShadowOfEdge());
snakeHackLayout.setMinVelocity(SnakeConfigReader.get().minVelocity());
snakeHackLayout.setOnlyListenToFastSwipe(SnakeConfigReader.get().onlyListenToFastSwipe());
snakeHackLayout.setShadowStartColor(SnakeConfigReader.get().shadowStartColor());
snakeHackLayout.setShadowEndColor(SnakeConfigReader.get().shadowEndColor());
snakeHackLayout.enableSwipeUpToHome(SnakeConfigReader.get().swipeUpToHomeEnabled());
snakeHackLayout.setAllowPageLinkageOfUIConfig(SnakeConfigReader.get().allowPageLinkage());
}
}
/**
* Open DragToClose for activity, just for internal using.
*
* @param activity the specified activity
*/
private static void openDragToCloseForActivity(@NonNull final Activity activity) {
replacedertActivityDestroyed(activity);
EnableDragToClose enableDragToClose = activity.getClreplaced().getAnnotation(EnableDragToClose.clreplaced);
if (null == enableDragToClose || !enableDragToClose.value())
return;
ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
View topWindowView = decorView.getChildAt(0);
// Just return if top window view was SnakeHackLayout.
if (topWindowView instanceof SnakeHackLayout)
return;
// Set transparent background to avoid flashing.
activity.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
activity.getWindow().getDecorView().setBackgroundDrawable(null);
TypedArray a = activity.getTheme().obtainStyledAttributes(new int[] { android.R.attr.windowBackground });
int background = a.getResourceId(0, 0);
a.recycle();
topWindowView.setBackgroundResource(background);
decorView.removeView(topWindowView);
final SnakeHackLayout snakeHackLayout = SnakeHackLayout.getLayout(activity, topWindowView, true);
decorView.addView(snakeHackLayout);
SetDragParameter dragParameter = activity.getClreplaced().getAnnotation(SetDragParameter.clreplaced);
setDragParameter(dragParameter, snakeHackLayout);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
snakeHackLayout.setOnlyListenToFastSwipe(true);
}
ActivityDragInterceptor.get(activity).attachToLayout(snakeHackLayout);
}
private static void replacedertActivityDestroyed(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && activity.isDestroyed()) {
throw new IllegalStateException("You cannot add this feature to a destroyed activity");
}
}
/**
* Turn the slide-off function on or off for activity.
*
* @param activity the specified activity
* @param enable true: turn on, false: turn off
*/
public static void enableDragToClose(@NonNull Activity activity, boolean enable) {
if (activity.isFinishing())
return;
EnableDragToClose enableDragToClose = activity.getClreplaced().getAnnotation(EnableDragToClose.clreplaced);
if (null == enableDragToClose) {
throw new SnakeConfigException("If you want to dynamically turn the slide-off feature on or off, add the EnableDragToClose annotation to " + activity.getClreplaced().getName() + " and set to true");
}
boolean isEnabled = Snake.dragToCloseEnabled(activity);
if (enable == isEnabled)
return;
ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
View topWindowView = decorView.getChildAt(0);
if (!(topWindowView instanceof SnakeHackLayout)) {
throw new SnakeConfigException("Did you enable the keep activities option in the settings? if not, commit issue please");
}
((SnakeHackLayout) topWindowView).ignoreDragEvent(!enable);
}
/**
* Turn the slide-off function on or off for fragment.
*
* @param fragment the specified fragment
* @param enable true: turn on, false: turn off
*/
public static void enableDragToClose(@NonNull android.app.Fragment fragment, boolean enable) {
if (fragment instanceof com.youngfeng.snake.app.Fragment) {
((Fragment) fragment).enableDragToClose(enable);
} else {
try {
Method method = fragment.getClreplaced().getMethod("enableDragToClose", Boolean.clreplaced);
method.invoke(fragment, enable);
} catch (Throwable e) {
if (e instanceof NoSuchMethodException) {
throw new SnakeConfigException("Plase use Snake.newProxy create a Fragment instance");
} else {
e.printStackTrace();
}
}
}
}
/**
* Turn the slide-off function on or off for support fragment.
*
* @param fragment the specified support fragment
* @param enable true: turn on, false: turn off
*/
public static void enableDragToClose(@NonNull androidx.fragment.app.Fragment fragment, boolean enable) {
if (fragment instanceof com.youngfeng.snake.androidx.app.Fragment) {
((com.youngfeng.snake.androidx.app.Fragment) fragment).enableDragToClose(enable);
} else {
try {
Method method = fragment.getClreplaced().getMethod("enableDragToClose", Boolean.clreplaced);
method.invoke(fragment, enable);
} catch (Throwable e) {
if (e instanceof NoSuchMethodException) {
throw new SnakeConfigException("Plase use Snake.newProxySupport create a Fragment instance");
} else {
e.printStackTrace();
}
}
}
}
/**
* Add OnDragListener for drag event.
*
* @param activity the current activity.
* @param onDragListener onDragListener
*/
public static void addDragListener(@NonNull Activity activity, Snake.OnDragListener onDragListener) {
ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
if (!(decorView.getChildAt(0) instanceof SnakeHackLayout) || null == onDragListener)
return;
((SnakeHackLayout) decorView.getChildAt(0)).addOnDragListener(onDragListener);
}
/**
* Add OnDragListener for drag event.
*
* @param fragment the current fragment.
* @param onDragListener onDragListener
*/
public static void addDragListener(@NonNull android.app.Fragment fragment, Snake.OnDragListener onDragListener) {
if (fragment instanceof com.youngfeng.snake.app.Fragment) {
((Fragment) fragment).addOnDragListener(onDragListener);
} else {
try {
Method method = fragment.getClreplaced().getMethod("addOnDragListener", OnDragListener.clreplaced);
method.invoke(fragment, onDragListener);
} catch (Throwable e) {
e.printStackTrace();
}
}
}
/**
* Add OnDragListener for drag event.
*
* @param fragment the current fragment.
* @param onDragListener onDragListener
*/
public static void addDragListener(@NonNull androidx.fragment.app.Fragment fragment, Snake.OnDragListener onDragListener) {
if (fragment instanceof com.youngfeng.snake.androidx.app.Fragment) {
((com.youngfeng.snake.androidx.app.Fragment) fragment).addOnDragListener(onDragListener);
} else {
try {
Method method = fragment.getClreplaced().getMethod("addOnDragListener", OnDragListener.clreplaced);
method.invoke(fragment, onDragListener);
} catch (Throwable e) {
e.printStackTrace();
}
}
}
/**
* Set custom touch interceptor for activity.
*
* @param activity the specified activity.
* @param interceptor the custom touch interceptor.
*/
public static void setCustomTouchInterceptor(@NonNull Activity activity, SnakeTouchInterceptor interceptor) {
if (activity.isFinishing() || null == interceptor)
return;
ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
if (!(decorView.getChildAt(0) instanceof SnakeHackLayout))
return;
SnakeHackLayout snakeHackLayout = (SnakeHackLayout) decorView.getChildAt(0);
snakeHackLayout.setCustomTouchInterceptor(interceptor);
}
/**
* Set custom touch interceptor for fragment.
*
* @param fragment the specified fragment.
* @param interceptor the custom touch interceptor.
*/
public static void setCustomTouchInterceptor(@NonNull android.app.Fragment fragment, SnakeTouchInterceptor interceptor) {
if (fragment instanceof com.youngfeng.snake.app.Fragment) {
((Fragment) fragment).setCustomTouchInterceptor(interceptor);
} else {
try {
Method method = fragment.getClreplaced().getMethod("setCustomTouchInterceptor", SnakeTouchInterceptor.clreplaced);
method.invoke(fragment, interceptor);
} catch (Throwable e) {
e.printStackTrace();
}
}
}
/**
* Set custom touch interceptor for fragment.
*
* @param fragment the specified fragment.
* @param interceptor the custom touch interceptor.
*/
public static void setCustomTouchInterceptor(@NonNull androidx.fragment.app.Fragment fragment, SnakeTouchInterceptor interceptor) {
if (fragment instanceof com.youngfeng.snake.androidx.app.Fragment) {
((com.youngfeng.snake.androidx.app.Fragment) fragment).setCustomTouchInterceptor(interceptor);
} else {
try {
Method method = fragment.getClreplaced().getMethod("setCustomTouchInterceptor", SnakeTouchInterceptor.clreplaced);
method.invoke(fragment, interceptor);
} catch (Throwable e) {
e.printStackTrace();
}
}
}
/**
* Wrap fragment animator to simplify use.
*
* @param superAnimator the result of call super onCreateAnimator
* @param animationController the interface of control fragment's animator
*
* @return the fragment animator
*/
public static Animator wrap(@Nullable Animator superAnimator, @NonNull SnakeAnimationController animationController) {
if (animationController.animationDisabled())
return AnimationFactory.emptyAnimator();
return superAnimator;
}
/**
* Wrap fragment animation to simplify use.
*
* @param superAnimation the result of call super onCreateAnimator
* @param animationController the interface of control fragment's animation
*
* @return the fragment animation
*/
public static Animation wrap(@Nullable Animation superAnimation, @NonNull SnakeAnimationController animationController) {
if (animationController.animationDisabled())
return AnimationFactory.emptyAmiation();
return superAnimation;
}
/**
* Disable fragment's pop animation.
*
* @param fragment the specified support fragment
* @param disable disable or enable animation
*/
public static void disableAnimation(androidx.fragment.app.Fragment fragment, boolean disable) {
if (fragment instanceof SnakeAnimationController) {
((SnakeAnimationController) fragment).disableAnimation(disable);
}
}
/**
* Disable fragment's pop animation.
*
* @param fragment the specified fragment
* @param disable disable or enable animation
*/
public static void disableAnimation(android.app.Fragment fragment, boolean disable) {
if (fragment instanceof SnakeAnimationController) {
((SnakeAnimationController) fragment).disableAnimation(disable);
}
}
/**
* Get enable status of the activity.
*
* @param activity the specified activity
* @return true: enabled false: disabled
*/
public static boolean dragToCloseEnabled(@NonNull Activity activity) {
replacedertActivityDestroyed(activity);
ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
View topWindowView = decorView.getChildAt(0);
if (topWindowView instanceof SnakeHackLayout) {
return !((SnakeHackLayout) topWindowView).ignoredDragEvent();
}
return false;
}
/**
* Get enable status of the fragment.
*
* @param fragment the specified fragment
* @return true: enabled false: disabled
*/
public static boolean dragToCloseEnabled(@NonNull android.app.Fragment fragment) {
View contentView = fragment.getView();
if (contentView instanceof SnakeHackLayout) {
return !((SnakeHackLayout) contentView).ignoredDragEvent();
}
return false;
}
/**
* Get enable status of the fragment.
*
* @param fragment the specified fragment
* @return true: enabled false: disabled
*/
public static boolean dragToCloseEnabled(@NonNull androidx.fragment.app.Fragment fragment) {
View contentView = fragment.getView();
if (contentView instanceof SnakeHackLayout) {
return !((SnakeHackLayout) contentView).ignoredDragEvent();
}
return false;
}
/**
* Enable or disable swipe up to home for the specified activity.
*
* @param activity the specified activity
* @param enable true: enable, false: disable
*/
public static void enableSwipeUpToHome(@NonNull Activity activity, boolean enable) {
if (activity.isFinishing())
return;
if (enable) {
EnableDragToClose enableDragToClose = activity.getClreplaced().getAnnotation(EnableDragToClose.clreplaced);
if (null == enableDragToClose || !enableDragToClose.value()) {
throw new SnakeConfigException("If you want to dynamically turn the swipe up to home feature on or off, add the EnableDragToClose annotation to " + activity.getClreplaced().getName() + " and set true.");
}
}
ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
View topWindowView = decorView.getChildAt(0);
if (!(topWindowView instanceof SnakeHackLayout)) {
throw new SnakeConfigException("Did you enable the keep activities option in the settings? if not, commit issue please");
}
((SnakeHackLayout) topWindowView).enableSwipeUpToHome(enable);
}
/**
* Enable or disable swipe up to home for the specified fragment.
*
* @param fragment the specified fragment
* @param enable true: enable, false: disable
*/
@Deprecated
public static void enableSwipeUpToHome(@NonNull android.app.Fragment fragment, boolean enable) {
if (fragment instanceof com.youngfeng.snake.app.Fragment) {
((Fragment) fragment).enableSwipeUpToHome(enable);
} else {
try {
Method method = fragment.getClreplaced().getMethod("enableSwipeUpToHome", Boolean.clreplaced);
method.invoke(fragment, enable);
} catch (Throwable e) {
if (e instanceof NoSuchMethodException) {
throw new SnakeConfigException("Please use Snake.newProxy create a Fragment instance");
} else {
e.printStackTrace();
}
}
}
}
/**
* Enable or disable swipe up to home for the specified support fragment.
*
* @param fragment the specified support fragment
* @param enable true: enable, false: disable
*/
@Deprecated
public static void enableSwipeUpToHome(@NonNull androidx.fragment.app.Fragment fragment, boolean enable) {
if (fragment instanceof com.youngfeng.snake.androidx.app.Fragment) {
((com.youngfeng.snake.androidx.app.Fragment) fragment).enableSwipeUpToHome(enable);
} else {
try {
Method method = fragment.getClreplaced().getMethod("enableSwipeUpToHome", Boolean.clreplaced);
method.invoke(fragment, enable);
} catch (Throwable e) {
if (e instanceof NoSuchMethodException) {
throw new SnakeConfigException("Please use Snake.newProxySupport create a Fragment instance");
} else {
e.printStackTrace();
}
}
}
}
/**
* Get enable state of the swipe up to home feature.
*
* @param activity the specified activity
*
* @return true: enable, false: disable
*/
public static boolean swipeUpToHomeEnabled(@NonNull Activity activity) {
replacedertActivityDestroyed(activity);
ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
View topWindowView = decorView.getChildAt(0);
if (topWindowView instanceof SnakeHackLayout) {
return ((SnakeHackLayout) topWindowView).swipeUpToHomeEnabled();
}
return false;
}
/**
* Get enable state of the swipe up to home feature.
*
* @param fragment the specified fragment
*
* @return true: enable, false: disable
*/
@Deprecated
public static boolean swipeUpToHomeEnabled(@NonNull android.app.Fragment fragment) {
View contentView = fragment.getView();
if (contentView instanceof SnakeHackLayout) {
return ((SnakeHackLayout) contentView).swipeUpToHomeEnabled();
}
return false;
}
/**
* Get enable state of the swipe up to home feature.
*
* @param fragment the specified fragment
*
* @return true: enable, false: disable
*/
@Deprecated
public static boolean swipeUpToHomeEnabled(@NonNull androidx.fragment.app.Fragment fragment) {
View contentView = fragment.getView();
if (contentView instanceof SnakeHackLayout) {
return ((SnakeHackLayout) contentView).swipeUpToHomeEnabled();
}
return false;
}
/**
* Control whether debug logging is enabled.
*/
public static void setDebug(boolean debug) {
Logger.debug = debug;
}
public static Context getContext() {
return mContext;
}
public static String versionName() {
return BuildConfig.VERSION_NAME;
}
public static int versionCode() {
return BuildConfig.VERSION_CODE;
}
public static boolean isSupportedForActivity() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
}
public static abstract clreplaced OnDragListener {
public void onDragStart(View view) {
}
public void onDrag(View view, int left, boolean isSetlling) {
}
public void onRelease(View view, float xVelocity) {
}
public void onBackToStartCompleted(View view) {
}
}
}
19
Source : Snake.java
with Apache License 2.0
from yuanhoujun
with Apache License 2.0
from yuanhoujun
/**
* Initializes snake with default configurations, it will find configuration file in replacedet/snake.xml,
* Snake will use default configruation if not found snake.xml.
*
* @param application application instance
*/
public static void init(Application application) {
application.registerActivityLifecycleCallbacks(new GlobalActivityLifecycleDelegate() {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
ActivityManager.get().put(activity);
openDragToCloseForActivity(activity);
Logger.d(activity.getClreplaced() + " onCreate completed...");
}
@Override
public void onActivityDestroyed(Activity activity) {
ActivityManager.get().remove(activity);
Logger.d(activity.getClreplaced() + " destoryed completed...");
}
});
SnakeConfigReader.get().init(application);
mContext = application;
}
19
Source : SnakeConfigReader.java
with Apache License 2.0
from yuanhoujun
with Apache License 2.0
from yuanhoujun
/**
* Snake config reader.
*
* @author Scott Smith 2017-12-19 08:41
*/
public clreplaced SnakeConfigReader {
private Application mApplication;
@SuppressLint("StaticFieldLeak")
private static SnakeConfigReader instance;
private SnakeConfig mSnakeConfig = new SnakeConfig();
private SnakeConfigReader() {
}
public static synchronized SnakeConfigReader get() {
if (null == instance) {
instance = new SnakeConfigReader();
}
return instance;
}
public void init(Application application) {
mApplication = application;
try {
InputStream inputStream = application.getreplacedets().open("snake.xml");
if (null != inputStream) {
readSnakeConfigIn(inputStream);
}
} catch (IOException e) {
// ignore this exception.
}
}
private void readSnakeConfigIn(InputStream inputStream) {
if (null == inputStream)
return;
XmlPullParser parser = Xml.newPullParser();
try {
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(inputStream, "utf-8");
parser.nextTag();
parser.nextTag();
parser.require(XmlPullParser.START_TAG, null, "config");
while (XmlPullParser.END_TAG != parser.next()) {
if (XmlPullParser.START_TAG != parser.getEventType()) {
continue;
}
String name = parser.getName();
parser.require(XmlPullParser.START_TAG, null, name);
String tagValue = readText(parser);
switch(name) {
case SnakeConfig.TAG_ENABLE_FOR_ROOT_ACTIVITY:
{
if (!"true".equals(tagValue) && !"false".equals(tagValue)) {
throw new SnakeConfigException("The tag " + name + " only allows the use of boolean values. eg: true or false, current value: " + tagValue);
}
mSnakeConfig.enableForRootActivity = Boolean.parseBoolean(tagValue);
break;
}
case SnakeConfig.TAG_ONLY_LISTEN_TO_FAST_SWIPE:
{
if (!"true".equals(tagValue) && !"false".equals(tagValue)) {
throw new SnakeConfigException("The tag " + name + " only allows the use of boolean values. eg: true or false, current value: " + tagValue);
}
mSnakeConfig.onlyListenToFastSwipe = Boolean.parseBoolean(tagValue);
break;
}
case SnakeConfig.TAG_MIN_VELOCITY:
{
try {
mSnakeConfig.minVelocity = Integer.parseInt(tagValue);
} catch (NumberFormatException e) {
throw new SnakeConfigException("The tag " + name + " only allows the use of integer values, current value: " + tagValue);
}
break;
}
case SnakeConfig.TAG_HIDE_SHADOW_OF_EDGE:
{
if (!"true".equals(tagValue) && !"false".equals(tagValue)) {
throw new SnakeConfigException("The tag " + name + " only allows the use of boolean values. eg: true or false, current value: " + tagValue);
}
mSnakeConfig.hideShadowOfEdge = Boolean.parseBoolean(tagValue);
break;
}
case SnakeConfig.TAG_SHADOW_START_COLOR:
{
try {
mSnakeConfig.shadowStartColor = Color.parseColor(tagValue);
} catch (IllegalArgumentException e) {
throw new SnakeConfigException("The tag " + name + " only allows the use of color string, eg: #ff0000, current value: " + tagValue);
}
break;
}
case SnakeConfig.TAG_SHADOW_END_COLOR:
{
try {
mSnakeConfig.shadowEndColor = Color.parseColor(tagValue);
} catch (IllegalArgumentException e) {
throw new SnakeConfigException("The tag " + name + " only allows the use of color string, eg: #ff0000, current value: " + tagValue);
}
break;
}
case SnakeConfig.TAG_ENABLE_SWIPE_UP_TO_HOME:
{
if (!"true".equals(tagValue) && !"false".equals(tagValue)) {
throw new SnakeConfigException("The tag " + name + " only allows the use of boolean values. eg: true or false, current value: " + tagValue);
}
mSnakeConfig.enableSwipeUpToHome = Boolean.parseBoolean(tagValue);
break;
}
case SnakeConfig.TAG_ALLOW_PAGE_LINKAGE:
{
if (!"true".equals(tagValue) && !"false".equals(tagValue)) {
throw new SnakeConfigException("The tag " + name + " only allows the use of boolean values. eg: true or false, current value: " + tagValue);
}
mSnakeConfig.allowPageLinkage = Boolean.parseBoolean(tagValue);
break;
}
}
parser.require(XmlPullParser.END_TAG, null, name);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
return result;
}
public boolean enableForRootActivity() {
return mSnakeConfig.enableForRootActivity;
}
public boolean onlyListenToFastSwipe() {
return mSnakeConfig.onlyListenToFastSwipe;
}
public int minVelocity() {
return mSnakeConfig.minVelocity;
}
public boolean hideShadowOfEdge() {
return mSnakeConfig.hideShadowOfEdge;
}
public int shadowStartColor() {
return mSnakeConfig.shadowStartColor;
}
public int shadowEndColor() {
return mSnakeConfig.shadowEndColor;
}
public boolean swipeUpToHomeEnabled() {
return mSnakeConfig.enableSwipeUpToHome;
}
public boolean allowPageLinkage() {
return mSnakeConfig.allowPageLinkage;
}
}
19
Source : SnakeConfigReader.java
with Apache License 2.0
from yuanhoujun
with Apache License 2.0
from yuanhoujun
public void init(Application application) {
mApplication = application;
try {
InputStream inputStream = application.getreplacedets().open("snake.xml");
if (null != inputStream) {
readSnakeConfigIn(inputStream);
}
} catch (IOException e) {
// ignore this exception.
}
}
19
Source : AppDelegateSingleton.java
with Apache License 2.0
from yinlingchaoliu
with Apache License 2.0
from yinlingchaoliu
/**
* 快速初始化
*
* @param app
*/
private void onInitSpeed(Application app) {
for (IApplicationDelegate appDelegate : mAppDelegateList) {
appDelegate.onInitSpeed(app);
}
}
19
Source : Rocket.java
with Apache License 2.0
from yinhaide
with Apache License 2.0
from yinhaide
/**
* 本类作用是暴露内部接口供外部使用
* Created by haide.yin([email protected]) on 2019/6/24 17:43.
*/
public clreplaced Rocket {
// 全局的Application
private static Application application;
/**
* applicatipn的初始化,建议在onCreate中调用
*
* @param app 进程
*/
public static void init(@NonNull Application app) {
// 存储当前的Application
application = app;
// 多国语言适配
LocaleHelper.init(application);
// 崩溃异常的处理
CrashHelper.getInstance().initCrash(application);
// 重写生命周期
application.registerActivityLifecycleCallbacks(new ActivityHelper());
}
/* ************************************************************* */
/* 国际化相关
/* ************************************************************* */
/**
* 用于支持应用内的多语言支持一,重写Application的attachBaseContext
* 使用方式:super.attachBaseContext(Rocket.attachBaseContext(context));
*
* @param context 上下文
*/
public static Context attachBaseContext(Context context) {
return LocaleHelper.attachBaseContext(context);
}
/**
* 用于支持应用内的多语言支持二,重写Application的onConfigurationChanged
* 使用方式:Rocket.onConfigurationChanged(application);
*
* @param app allication
*/
public static void onConfigurationChanged(Application app) {
LocaleHelper.init(app);
}
/**
* 设置APP需要缓存的语言
*
* @param locale 语言
*/
public static void setSaveLocale(Context context, Locale locale) {
LocaleHelper.setLocale(context, locale);
}
/**
* 读取APP需要缓存的语言
*
* @return locale
*/
public static Locale getSaveLocale(Context context) {
return LocaleHelper.getLocale(context);
}
/* ************************************************************* */
/* View反射注解相关
/* ************************************************************* */
/**
* 绑定ViewHolder,之后可以注解的方式获取绑定viewy以及事件注解.用法如下:
* ViewHolder(View itemView) {
* super(itemView);
* Rocket.bindViewHolder(this,itemView);//View注解
* }
* @param viewHolder viewHolder
* @param view itemView
*/
public static void bindViewHolder(Object viewHolder, View view) {
ViewInjectHelper.getInstance().injectViewHolder(viewHolder, view);
}
/**
* 绑定View 主要用户绑定Widget
*
* @param view itemView
*/
public static void bindView(View view) {
ViewInjectHelper.getInstance().injectView(view);
}
/* ************************************************************* */
/* Rocket日志相关
/* ************************************************************* */
/**
* 开放指定类型的Log,默认全开
*
* @param logtype Log类型
*/
public static void setLogType(RoLogUtil.LOGTYPE logtype) {
RoLogUtil.openLogType(logtype);
}
/**
* 写入Log信息
*
* @param logString Log信息
*/
public static void writeOuterLog(String logString) {
Activity topActivity = getTopActivity();
if (topActivity != null && !topActivity.isFinishing()) {
RecordHelper.writeOuterLog(topActivity, logString);
}
}
/* ************************************************************* */
/* Application 、 Activity 、 Fragment 相关
/* ************************************************************* */
/**
* 读取当前的Application
*
* @return Application
*/
public static Application getApplication() {
return application;
}
/**
* 读取当前的栈顶的Activity
*
* @return TOP_ACTIVITY
*/
public static RoActivity getTopActivity() {
return (RoActivity) ActivityHelper.getTopActivity();
}
/**
* 全局显示悬浮球以及栈视图
*/
public static void showGloaBall() {
FragRouterWidget fragRouterWidget1 = new FragRouterWidget(getTopActivity());
fragRouterWidget1.showStackView();
WidgetUtil.showGloaBal(getTopActivity(), fragRouterWidget1, FragRouterWidget.ROUTER_WIDGET_ID, -1, null);
}
/* ************************************************************* */
/* 状态栏相关
/* ************************************************************* */
/**
* 设置状态栏颜色
*
* @param color 状态栏颜色值
* @param isImmersion 是否是沉浸式风格,隐藏状态栏创建自定义状态栏
*/
public static void setStatusBarColor(Activity activity, @ColorInt int color, boolean isImmersion) {
StatusBarHelper.setStatusBarColor(activity, color, isImmersion);
}
/**
* 使指定的View向下Padding偏移一个状态栏高度,留出状态栏空间,主要用于设置沉浸式后,页面顶到顶端有突兀感
*
* @param targetView 需要偏移的View
* @param enable 开启或者关闭
* @param isPaddingOrMargin 向下偏移是padding还是margin,true的话是padding,false的话是margin
*/
public static void setOffsetStatusBar(Activity activity, View targetView, boolean enable, boolean isPaddingOrMargin) {
StatusBarHelper.setOffsetStatusBar(activity, targetView, enable, isPaddingOrMargin);
}
/* ************************************************************* */
/* 栈的操作
/* ************************************************************* */
/**
* 读取当前的Fragment是否是处于栈顶,防止在后台跳转的情况
*
* @return boolean 是否处于栈顶
*/
public static boolean isTopRocketStack(String fragmentTag) {
RoActivity activity = getTopActivity();
if (activity != null) {
return activity.getStack().isTopRocketStack(fragmentTag);
}
return false;
}
/**
* 读取当前的Fragment
*/
public static Fragment getTopRocketStackFragment(RoActivity activity) {
return FragHelper.getInstance().getTopRocketStackFragment(activity);
}
/* ************************************************************* */
/* 实用工具类
/* ************************************************************* */
/**
* Android7.0以后,不同应用之间不允许传递Uri链接,有两种方式可以实现uri的传递
* 绕过版本限制,删除Uri的检测
*/
public static void detectFileUriExposure() {
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
builder.detectFileUriExposure();
}
}
/**
* 显示状态栏,这句话配合APP启动配置的全屏window主题
* <item name="android:windowFullscreen">true</item>
*/
public static void clearWindowFullscreen(Activity activity) {
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
19
Source : ILoginProviderImpl.java
with Apache License 2.0
from yifei8
with Apache License 2.0
from yifei8
/**
* 类描述:
* 创建人:yifei
* 创建时间:2018/5/24
* 修改人:
* 修改时间:
* 修改备注:
*/
@Route(path = "/login-module/ILoginProviderImpl")
public clreplaced ILoginProviderImpl implements ILoginProvider {
private String para1;
private int para2;
private Application application;
public ILoginProviderImpl(String para1, int para2, Application application) {
this.para1 = para1;
this.para2 = para2;
this.application = application;
}
@Override
public String login() {
Routerfit.register(RouteService.clreplaced).launchLoginActivity(null);
return "ILoginProviderImpl para1:" + para1 + ",para2:" + para2 + ",application:" + application.getPackageCodePath();
}
}
19
Source : Routerfit.java
with Apache License 2.0
from yifei8
with Apache License 2.0
from yifei8
public static void init(@NonNull Application application) {
ActivityLifecycleMonitor.init(application);
}
19
Source : AppContext.java
with Apache License 2.0
from YeDaxia
with Apache License 2.0
from YeDaxia
/**
* @author Darcy https://yedaxia.github.io/
* @version 2018/2/4.
*/
public clreplaced AppContext {
private static final String SD_ROOT_PATH = "MusicNotePlus";
private static Application sApplication;
private static DaoSession daoSession;
static void initContext(Application app) {
sApplication = app;
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(app, "music-note-db", null);
DaoMaster daoMaster = new DaoMaster(helper.getWritableDb());
ZXingLibrary.initDisplayOpinion(app);
daoSession = daoMaster.newSession();
}
/**
* 获取音频的临时存放目录
* @return
*/
public static File getAudioTempPath() {
return FileUtils.mkDirs(new File(rootSdPath(), "Audio"));
}
/**
* 音频输出目录
* @return
*/
public static File getAudioOutPath() {
return FileUtils.mkDirs(new File(rootSdPath(), "Output"));
}
private static File rootSdPath() {
return FileUtils.mkDirs(new File(Environment.getExternalStorageDirectory(), SD_ROOT_PATH));
}
/**
* 获取Application对象
* @return
*/
public static Application getApplication() {
return sApplication;
}
public static DaoSession getDaoSession() {
return daoSession;
}
}
19
Source : AppContext.java
with Apache License 2.0
from YeDaxia
with Apache License 2.0
from YeDaxia
static void initContext(Application app) {
sApplication = app;
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(app, "music-note-db", null);
DaoMaster daoMaster = new DaoMaster(helper.getWritableDb());
ZXingLibrary.initDisplayOpinion(app);
daoSession = daoMaster.newSession();
}
19
Source : YUtils.java
with Apache License 2.0
from yechaoa
with Apache License 2.0
from yechaoa
/**
* Created by yechao on 2017/4/2.
* Describe : 快速开发工具集合
* <p>
* GitHub : https://github.com/yechaoa
* CSDN : http://blog.csdn.net/yechaoa
*/
public clreplaced YUtils {
private static Toast toast;
private static ProgressDialog progressDialog;
private static Application mApp = null;
/**
* 使用init()即可
*/
@Deprecated
public static void initialize(Application app) {
mApp = app;
app.registerActivityLifecycleCallbacks(ActivityUtil.getActivityLifecycleCallbacks());
}
public static void init(Application app) {
mApp = app;
app.registerActivityLifecycleCallbacks(ActivityUtil.getActivityLifecycleCallbacks());
}
/**
* 使用getApp()即可
*/
@Deprecated
public static Application getApplication() {
return mApp;
}
public static Application getApp() {
if (null == mApp) {
throw new NullPointerException("YUtils未在Application中初始化");
} else {
return mApp;
}
}
/**
* 获取屏幕宽度 改为DisplayUtil.getScreenWidth()
*/
@Deprecated
public static int getScreenWidth() {
DisplayMetrics dm = new DisplayMetrics();
ActivityUtil.getCurrentActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
return dm.widthPixels;
}
/**
* 获取屏幕高度 改为DisplayUtil.getScreenHeight()
*/
@Deprecated
public static int getScreenHeight() {
DisplayMetrics dm = new DisplayMetrics();
ActivityUtil.getCurrentActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
return dm.heightPixels;
}
/**
* Loading加载框
*/
public static void showLoading(String msg) {
progressDialog = ProgressDialog.show(ActivityUtil.getCurrentActivity(), "", msg, true, true);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
}
public static void showLoading(Activity activity, String msg) {
progressDialog = ProgressDialog.show(activity, "", msg, true, true);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
}
/**
* hideLoading
*/
public static void hideLoading() {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
}
}
/**
* loading是否显示,需在showLoading()之后调用,否则为false
*/
public static boolean loadingIsShowing() {
if (progressDialog == null) {
return false;
} else {
return progressDialog.isShowing();
}
}
/**
* 根据时间休眠然后关闭当前页面
* 比如:5秒自动返回
* 或者只需要后台给一个结果而已
*/
public static void finishBySleep(final long millis) {
new Thread() {
@Override
public void run() {
try {
Thread.sleep(millis);
ActivityUtil.getCurrentActivity().finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
/**
* 获取版本名
*/
public static String getVersionName() {
try {
PackageManager packageManager = YUtils.getApp().getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(YUtils.getApp().getPackageName(), 0);
return packageInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return null;
}
}
/**
* 获取版本号
*/
public static int getVersionCode() {
try {
PackageManager packageManager = YUtils.getApp().getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(YUtils.getApp().getPackageName(), 0);
return packageInfo.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return 0;
}
}
/**
* 检验手机号
*/
public static boolean checkPhoneNumber(String number) {
Pattern p = null;
Matcher m = null;
boolean b = false;
p = Pattern.compile("^[1][3,4,5,6,7,8,9][0-9]{9}$");
m = p.matcher(number);
b = m.matches();
return b;
}
/**
* MD5加密
*/
public static String MD5(String data) {
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
md5.update(data.getBytes());
byte[] m = md5.digest();
return Base64.encodeToString(m, Base64.DEFAULT);
}
/**
* dp2px 改为DisplayUtil.dp2px()
*/
@Deprecated
public static int dp2px(float dp) {
float density = YUtils.getApp().getResources().getDisplayMetrics().density;
return (int) (dp * density + 0.5f);
}
/**
* px2dp 改为DisplayUtil.px2dp()
*/
@Deprecated
public static float px2dp(int px) {
float density = YUtils.getApp().getResources().getDisplayMetrics().density;
return px / density;
}
/**
* 复制文本到粘贴板
*/
public static void copyToClipboard(String text) {
ClipboardManager cm = (ClipboardManager) YUtils.getApp().getSystemService(Activity.CLIPBOARD_SERVICE);
cm.setPrimaryClip(ClipData.newPlainText(YUtils.getApp().getPackageName(), text));
}
/**
* 字体高亮
*/
public static View foreground(View view, int color, int start, int end) {
if (view instanceof Button) {
Button btn = (Button) view;
// 获取文字
Spannable span = new SpannableString(btn.getText().toString());
// 设置颜色和起始位置
span.setSpan(new ForegroundColorSpan(color), start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
btn.setText(span);
return btn;
} else if (view instanceof TextView) {
// EditText extends TextView
TextView text = (TextView) view;
Spannable span = new SpannableString(text.getText().toString());
span.setSpan(new ForegroundColorSpan(color), start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
text.setText(span);
return text;
}
return null;
}
/**
* 弹出软键盘
*/
public static void showSoftKeyboard(View view) {
InputMethodManager inputManger = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManger.showSoftInput(view, InputMethodManager.SHOW_FORCED);
}
/**
* 关闭软键盘
*/
public static void closeSoftKeyboard() {
InputMethodManager inputManger = (InputMethodManager) ActivityUtil.getCurrentActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManger.hideSoftInputFromWindow(ActivityUtil.getCurrentActivity().getWindow().getDecorView().getWindowToken(), 0);
}
/**
* 是否有sim卡 即设备是否可以拨打电话等
*/
public static Boolean hreplacedim() {
TelephonyManager telephonyManager = (TelephonyManager) getApp().getSystemService(Context.TELEPHONY_SERVICE);
boolean result = true;
switch(telephonyManager.getSimState()) {
case TelephonyManager.SIM_STATE_ABSENT:
case TelephonyManager.SIM_STATE_UNKNOWN:
result = false;
break;
}
return result;
}
}
19
Source : ViewModelFactory.java
with MIT License
from YassinAJDI
with MIT License
from YassinAJDI
/**
* A creator is used to inject the sandwich ID into the ViewModel
*/
public clreplaced ViewModelFactory extends ViewModelProvider.NewInstanceFactory {
private final Application mApplication;
private final int mPosition;
public static ViewModelFactory getInstance(Application application, int position) {
return new ViewModelFactory(application, position);
}
private ViewModelFactory(Application application, int position) {
mApplication = application;
mPosition = position;
}
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Clreplaced<T> modelClreplaced) {
if (modelClreplaced.isreplacedignableFrom(SandwichViewModel.clreplaced)) {
// noinspection unchecked
return (T) new SandwichViewModel(mApplication, mPosition);
}
throw new IllegalArgumentException("Unknown ViewModel clreplaced: " + modelClreplaced.getName());
}
}
19
Source : ViewModelFactory.java
with MIT License
from YassinAJDI
with MIT License
from YassinAJDI
public static ViewModelFactory getInstance(Application application, int position) {
return new ViewModelFactory(application, position);
}
19
Source : ProxyApplication.java
with Apache License 2.0
from yangkun19921001
with Apache License 2.0
from yangkun19921001
public clreplaced ProxyApplication extends Application {
// 定义好解密后的文件的存放路径
private String app_name;
private String app_version;
private String TAG = this.getClreplaced().getSimpleName();
private long endTime;
/**
* ActivityThread创建Application之后调用的第一个方法
* 可以在这个方法中进行解密,同时把dex交给android去加载
*/
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
// 用于在解密过程中如果出现异常,可以进行捕获
CrashHandler.getInstance().init(base);
long startTime = SystemClock.currentThreadTimeMillis();
// 1. 获取用户填入的metadata
getMetaData();
// 2. 得到当前加密了的APK文件,也就是安装包
// File apkFile=new File(Environment.getExternalStorageDirectory()+"/app-signed-aligned.apk");
File apkFile = new File(getApplicationInfo().sourceDir);
// 3. 把apk解压 app_name+"_"+app_version 目录中的内容需要 boot 权限才能用
File versionDir = getDir("DevYK", Context.MODE_PRIVATE);
File appDir = new File(versionDir, "app");
File dexDir = new File(appDir, "dexDir");
// 4. 得到我们需要加载的Dex文件
List<File> dexFiles = new ArrayList<>();
// 进行解密(最好做MD5文件校验)
if (!dexDir.exists() || dexDir.list().length == 0) {
// 把apk解压到appDir
Zip.unZip(apkFile, appDir);
// 获取目录下所有的文件
File[] files = appDir.listFiles();
for (File file : files) {
String name = file.getName();
if (name.endsWith(".dex") && !TextUtils.equals(name, "clreplacedes.dex")) {
try {
// 读取文件内容
byte[] bytes = ProxyUtils.getBytes(file);
// 5. 解密 dex
byte[] decrypt = EncryptUtil.decrypt(bytes, EncryptUtil.ivBytes);
// 写到指定的目录
FileOutputStream fos = new FileOutputStream(file);
fos.write(decrypt);
fos.flush();
fos.close();
dexFiles.add(file);
} catch (Exception e) {
e.printStackTrace();
}
}
}
} else {
for (File file : dexDir.listFiles()) {
dexFiles.add(file);
}
}
try {
// 6.把解密后的文件加载到系统
loadDex(dexFiles, versionDir);
endTime = SystemClock.currentThreadTimeMillis() - startTime;
Log.d(TAG, "解密完成! 共耗时:" + endTime + " ms");
} catch (Exception e) {
e.printStackTrace();
}
}
private void loadDex(List<File> dexFiles, File versionDir) throws Exception {
// 1.先从 ClreplacedLoader 中获取 pathList 的变量
Field pathListField = ProxyUtils.findField(getClreplacedLoader(), "pathList");
// 1.1 得到 DexPathList 类
Object pathList = pathListField.get(getClreplacedLoader());
// 1.2 从 DexPathList 类中拿到 dexElements 变量
Field dexElementsField = ProxyUtils.findField(pathList, "dexElements");
// 1.3 拿到已加载的 dex 数组
Object[] dexElements = (Object[]) dexElementsField.get(pathList);
// 2. 反射到初始化 dexElements 的方法,也就是得到加载 dex 到系统的方法
Method makeDexElements = ProxyUtils.findMethod(pathList, "makePathElements", List.clreplaced, File.clreplaced, List.clreplaced);
// 2.1 实例化一个 集合 makePathElements 需要用到
ArrayList<IOException> suppressedExceptions = new ArrayList<IOException>();
// 2.2 反射执行 makePathElements 函数,把已解码的 dex 加载到系统,不然是打不开 dex 的,会导致 crash
Object[] addElements = (Object[]) makeDexElements.invoke(pathList, dexFiles, versionDir, suppressedExceptions);
// 3. 实例化一个新数组,用于将当前加载和已加载的 dex 合并成一个新的数组
Object[] newElements = (Object[]) Array.newInstance(dexElements.getClreplaced().getComponentType(), dexElements.length + addElements.length);
// 3.1 将系统中的已经加载的 dex 放入 newElements 中
System.arraycopy(dexElements, 0, newElements, 0, dexElements.length);
// 3.2 将解密后已加载的 dex 放入新数组中
System.arraycopy(addElements, 0, newElements, dexElements.length, addElements.length);
// 4. 将合并的新数组重新设置给 DexPathList的 dexElements
dexElementsField.set(pathList, newElements);
}
private void getMetaData() {
try {
ApplicationInfo applicationInfo = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
Bundle metaData = applicationInfo.metaData;
if (null != metaData) {
if (metaData.containsKey("app_name")) {
app_name = metaData.getString("app_name");
}
if (metaData.containsKey("app_version")) {
app_version = metaData.getString("app_version");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 开始替换application
*/
@Override
public void onCreate() {
super.onCreate();
try {
Toast.makeText(getApplicationContext(), "解密完成! 共耗时:" + endTime + " ms", Toast.LENGTH_LONG).show();
bindRealApplicatin();
} catch (Exception e) {
e.printStackTrace();
}
}
boolean isBindReal;
Application delegate;
private void bindRealApplicatin() throws Exception {
if (isBindReal) {
return;
}
if (TextUtils.isEmpty(app_name)) {
return;
}
// 1. 得到 attachBaseContext(context) 传入的上下文 ContextImpl
Context baseContext = getBaseContext();
// 2. 拿到真实 APK APPlication 的 clreplaced
Clreplaced<?> delegateClreplaced = Clreplaced.forName(app_name);
// 3. 反射实例化,其实 Android 中四大组件都是这样实例化的。
delegate = (Application) delegateClreplaced.newInstance();
// 3.1 得到 Application attach() 方法 也就是最先初始化的
Method attach = Application.clreplaced.getDeclaredMethod("attach", Context.clreplaced);
attach.setAccessible(true);
// 执行 Application#attach(Context)
// 3.2 将真实的 Application 和假的 Application 进行替换。想当于自己手动控制 真实的 Application 生命周期
attach.invoke(delegate, baseContext);
// ContextImpl---->mOuterContext(app) 通过Application的attachBaseContext回调参数获取
// 4. 拿到 Context 的实现类
Clreplaced<?> contextImplClreplaced = Clreplaced.forName("android.app.ContextImpl");
// 4.1 获取 mOuterContext Context 属性
Field mOuterContextField = contextImplClreplaced.getDeclaredField("mOuterContext");
mOuterContextField.setAccessible(true);
// 4.2 将真实的 Application 交于 Context 中。这个根据源码执行,实例化 Application 下一个就行调用 setOuterContext 函数,所以需要绑定 Context
// app = mActivityThread.mInstrumentation.newApplication(
// cl, appClreplaced, appContext);
// appContext.setOuterContext(app);
mOuterContextField.set(baseContext, delegate);
// ActivityThread--->mAllApplications(ArrayList) ContextImpl的mMainThread属性
// 5. 拿到 ActivityThread 变量
Field mMainThreadField = contextImplClreplaced.getDeclaredField("mMainThread");
mMainThreadField.setAccessible(true);
// 5.1 拿到 ActivityThread 对象
Object mMainThread = mMainThreadField.get(baseContext);
// ActivityThread--->>mInitialApplication
// 6. 反射拿到 ActivityThread clreplaced
Clreplaced<?> activityThreadClreplaced = Clreplaced.forName("android.app.ActivityThread");
// 6.1 得到当前加载的 Application 类
Field mInitialApplicationField = activityThreadClreplaced.getDeclaredField("mInitialApplication");
mInitialApplicationField.setAccessible(true);
// 6.2 将 ActivityThread 中的 Applicaiton 替换为 真实的 Application 可以用于接收相应的声明周期和一些调用等
mInitialApplicationField.set(mMainThread, delegate);
// ActivityThread--->mAllApplications(ArrayList) ContextImpl的mMainThread属性
// 7. 拿到 ActivityThread 中所有的 Application 集合对象,这里是多进程的场景
Field mAllApplicationsField = activityThreadClreplaced.getDeclaredField("mAllApplications");
mAllApplicationsField.setAccessible(true);
ArrayList<Application> mAllApplications = (ArrayList<Application>) mAllApplicationsField.get(mMainThread);
// 7.1 删除 ProxyApplication
mAllApplications.remove(this);
// 7.2 添加真实的 Application
mAllApplications.add(delegate);
// LoadedApk------->mApplication ContextImpl的mPackageInfo属性
// 8. 从 ContextImpl 拿到 mPackageInfo 变量
Field mPackageInfoField = contextImplClreplaced.getDeclaredField("mPackageInfo");
mPackageInfoField.setAccessible(true);
// 8.1 拿到 LoadedApk 对象
Object mPackageInfo = mPackageInfoField.get(baseContext);
// 9 反射得到 LoadedApk 对象
// @Override
// public Context getApplicationContext() {
// return (mPackageInfo != null) ?
// mPackageInfo.getApplication() : mMainThread.getApplication();
// }
Clreplaced<?> loadedApkClreplaced = Clreplaced.forName("android.app.LoadedApk");
Field mApplicationField = loadedApkClreplaced.getDeclaredField("mApplication");
mApplicationField.setAccessible(true);
// 9.1 将 LoadedApk 中的 Application 替换为 真实的 Application
mApplicationField.set(mPackageInfo, delegate);
// 修改ApplicationInfo clreplacedName LooadedApk
// 10. 拿到 LoadApk 中的 mApplicationInfo 变量
Field mApplicationInfoField = loadedApkClreplaced.getDeclaredField("mApplicationInfo");
mApplicationInfoField.setAccessible(true);
// 10.1 根据变量反射得到 ApplicationInfo 对象
ApplicationInfo mApplicationInfo = (ApplicationInfo) mApplicationInfoField.get(mPackageInfo);
// 10.2 将我们真实的 APPlication ClreplacedName 名称赋值于它
mApplicationInfo.clreplacedName = app_name;
// 11. 执行 代理 Application onCreate 声明周期
delegate.onCreate();
// 解码完成
isBindReal = true;
}
/**
* 让代码走入if中的第三段中
* @return
*/
@Override
public String getPackageName() {
if (!TextUtils.isEmpty(app_name)) {
return "";
}
return super.getPackageName();
}
/**
* 这个函数是如果在 AndroidManifest.xml 中定义了 ContentProvider 那么就会执行此处 : installProvider,简介调用该函数
* @param packageName
* @param flags
* @return
* @throws PackageManager.NameNotFoundException
*/
@Override
public Context createPackageContext(String packageName, int flags) throws PackageManager.NameNotFoundException {
if (TextUtils.isEmpty(app_name)) {
return super.createPackageContext(packageName, flags);
}
try {
bindRealApplicatin();
} catch (Exception e) {
e.printStackTrace();
}
return delegate;
}
}
19
Source : ArticleListViewModelTest.java
with MIT License
from yangjiantao
with MIT License
from yangjiantao
@Before
public void setup() {
mRepo = mock(WanAndroidArticleRepository.clreplaced);
mTestValue = new MutableLiveData<>();
Application app = mock(Application.clreplaced);
when(mRepo.getSelectedArticles()).thenReturn(mTestValue);
mViewModel = new ArticleListViewModel(app, mRepo);
}
19
Source : X5WebUtils.java
with Apache License 2.0
from yangchong211
with Apache License 2.0
from yangchong211
/**
* <pre>
* @author yangchong
* blog : https://github.com/yangchong211
* time : 2019/9/10
* desc : WebView工具类
* revise: demo地址:https://github.com/yangchong211/YCWebView
* </pre>
*/
public final clreplaced X5WebUtils {
/**
* 全局上下文
*/
private static Application application;
/**
* 初始化,是否开启https+dns优化
* 具体看文档:https://help.aliyun.com/doreplacedent_detail/150879.html?spm=a2c4g.11174283.3.2.4a41110cCH6pDw
* 可以看Android接入文档,已经用于实践,默认是不开启的
*/
public static boolean isHttpDns = false;
/**
* accountID
*/
public static String accountID = null;
/**
* 需要使用https+dns优化的链接url的host集合
*/
public static ArrayList<String> host = null;
/**
* 不能直接new,否则抛个异常
*/
private X5WebUtils() throws WebViewException {
throw new WebViewException(1, "u can't instantiate me...");
}
/**
* 初始化腾讯x5浏览器webView,建议在application初始化
* @param context 上下文
*/
public static void init(Context context) {
if (context instanceof Application) {
application = (Application) context;
// 搜集本地tbs内核信息并上报服务器,服务器返回结果决定使用哪个内核。
QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {
@Override
public void onViewInitFinished(boolean arg0) {
// x5內核初始化完成的回调,为true表示x5内核加载成功
// 否则表示x5内核加载失败,会自动切换到系统内核。
X5LogUtils.i("app" + " onViewInitFinished is " + arg0);
}
@Override
public void onCoreInitFinished() {
X5LogUtils.i("app" + "onCoreInitFinished ");
}
};
// x5内核初始化接口
QbSdk.initX5Environment(context, cb);
ToastUtils.init(application);
X5WebView.isLongClick = true;
X5LogUtils.setIsLog(true);
} else {
throw new UnsupportedOperationException("context must be application...");
}
}
/**
* 初始化缓存
* @param application 上下文
*/
public static void initCache(Application application, String path) {
if (path == null || path.length() == 0) {
path = "YcCacheWebView";
}
// 1.创建委托对象
WebViewCacheDelegate webViewCacheDelegate = WebViewCacheDelegate.getInstance();
// 2.创建调用处理器对象,实现类
WebViewCacheWrapper.Builder builder = new WebViewCacheWrapper.Builder(application);
// 设置缓存路径,默认getCacheDir,名称CacheWebViewCache
builder.setCachePath(new File(application.getCacheDir().toString(), path)).setCacheSize(1024 * 1024 * 100).setConnectTimeoutSecond(20).setReadTimeoutSecond(20).setCacheType(WebCacheType.FORCE);
webViewCacheDelegate.init(builder);
}
/**
* 初始化Http+Dns解析的属性
* @param httpDns 是否使用
* @param account accountID
* @param hostUrl host
*/
public static void setHttpDns(boolean httpDns, String account, ArrayList<String> hostUrl) {
isHttpDns = httpDns;
accountID = account;
host = hostUrl;
}
/**
* 获取全局上下文
* @return 上下文
*/
public static Application getApplication() {
return application;
}
/**
* 判断当前url是否在白名单中
* @param arrayList 白名单集合
* @param url url
* @return
*/
public static boolean isWhiteList(ArrayList<String> arrayList, String url) {
if (url == null) {
return false;
}
if (arrayList == null || arrayList.size() == 0) {
return false;
}
// 重要提醒:建议只使用https协议通信,避免中间人攻击
if (!url.startsWith("http://") && !url.startsWith("https://")) {
return false;
}
// 提取host
String host = "";
try {
// 提取host,如果需要校验Path可以通过url.getPath()获取
host = Uri.parse(url).getHost();
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < arrayList.size(); i++) {
if (host != null && host.equals(arrayList.get(i))) {
// 是咱们自己的host
return true;
}
}
// 不在白名单内
return false;
}
/**
* 判断网络是否连接
* <p>需添加权限
* {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />}</p>
*
* @return {@code true}: 是<br>{@code false}: 否
*/
public static boolean isConnected(Context context) {
if (context == null) {
return false;
}
NetworkInfo info = getActiveNetworkInfo(context);
return info != null && info.isConnected();
}
/**
* 获取活动网络信息
* <p>需添加权限
* {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />}</p>
*
* @return NetworkInfo
*/
@SuppressLint("MissingPermission")
private static NetworkInfo getActiveNetworkInfo(Context context) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (manager == null) {
return null;
}
return manager.getActiveNetworkInfo();
}
/**
* Return whether the activity is alive.
*
* @param context The context.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isActivityAlive(final Context context) {
return isActivityAlive(getActivityByContext(context));
}
/**
* Return the activity by context.
*
* @param context The context.
* @return the activity by context.
*/
public static Activity getActivityByContext(Context context) {
if (context instanceof Activity) {
return (Activity) context;
}
while (context instanceof ContextWrapper) {
if (context instanceof Activity) {
return (Activity) context;
}
context = ((ContextWrapper) context).getBaseContext();
}
return null;
}
/**
* Return whether the activity is alive.
*
* @param activity The activity.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isActivityAlive(final Activity activity) {
return activity != null && !activity.isFinishing() && (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1 || !activity.isDestroyed());
}
/**
* 注解限定符
*/
@IntDef({ ErrorMode.NO_NET, ErrorMode.STATE_404, ErrorMode.RECEIVED_ERROR, ErrorMode.SSL_ERROR, ErrorMode.TIME_OUT, ErrorMode.STATE_500, ErrorMode.ERROR_PROXY })
@Retention(RetentionPolicy.SOURCE)
public @interface ErrorType {
}
/**
* 异常状态模式
* NO_NET 没有网络
* STATE_404 404,网页无法打开
* RECEIVED_ERROR onReceivedError,请求网络出现error
* SSL_ERROR 在加载资源时通知主机应用程序发生SSL错误
* TIME_OUT 网络连接超时
* STATE_500 服务器异常
* ERROR_PROXY 代理异常
*/
@Retention(RetentionPolicy.SOURCE)
public @interface ErrorMode {
int NO_NET = 1001;
int STATE_404 = 1002;
int RECEIVED_ERROR = 1003;
int SSL_ERROR = 1004;
int TIME_OUT = 1005;
int STATE_500 = 1006;
int ERROR_PROXY = 1007;
}
/**
* 判断是否为重定向url
* @param url 原始链接
* @return True 为重定向链接
*/
public static boolean shouldSkipUrl(@Nullable String url) {
if (TextUtils.isEmpty(url)) {
return true;
}
Uri uri = Uri.parse(url);
final String host = uri.getHost();
// skip redirect
if (!TextUtils.isEmpty(getKdtUnionUrl(uri))) {
return true;
}
// skip 'about:blank'
if (TextUtils.isEmpty(host)) {
return true;
}
return false;
}
@Nullable
private static String getKdtUnionUrl(@NonNull Uri uri) {
return uri.isOpaque() ? null : uri.getQueryParameter("redirect_uri");
}
public static boolean isMainThread() {
return Looper.getMainLooper() == Looper.myLooper();
}
}
19
Source : ToastUtils.java
with Apache License 2.0
from yangchong211
with Apache License 2.0
from yangchong211
/**
* <pre>
* @author yangchong
* blog : https://github.com/yangchong211
* time : 2017/06/4
* desc : Toast工具类
* revise: 具体看GitHub开源项目:https://github.com/yangchong211/YCDialog
* </pre>
*/
public final clreplaced ToastUtils {
@SuppressLint("StaticFieldLeak")
private static Application mApp;
private static SoftReference<Toast> mToast;
public static int background = R.drawable.shape_toast_bg_r10;
/**
* 初始化吐司工具类
* @param app 应用
*/
public static void init(@NonNull final Application app) {
mApp = app;
}
/**
* 私有构造
*/
private ToastUtils() {
// 避免初始化
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* 检查上下文不能为空,必须先进性初始化操作
*/
private static void checkContext() {
if (mApp == null) {
throw new NullPointerException("ToastUtils context is not null,please first init");
}
}
/**
* 吐司工具类 避免点击多次导致吐司多次,最后导致Toast就长时间关闭不掉了
* 注意:这里如果传入context会报内存泄漏;传递activity..getApplicationContext()
* @param content 吐司内容
*/
private static Toast toast;
@SuppressLint("ShowToast")
public static void showToast(String content) {
checkMainThread();
checkContext();
if (!checkNull(mToast)) {
mToast.get().cancel();
}
Toast toast = Toast.makeText(mApp, "", Toast.LENGTH_SHORT);
toast.setText(content);
toast.show();
mToast = new SoftReference<>(toast);
}
/**
* 某些系统可能屏蔽通知
* 1:检查 SystemUtils.isEnableNotification(BaseApplication.getApplication());
* 2:替代方案 SnackBarUtils.showSnack(topActivity, noticeStr);
* 圆角
* 屏幕中间
* @param notice 内容
*/
public static void showRoundRectToast(CharSequence notice) {
checkMainThread();
checkContext();
if (TextUtils.isEmpty(notice)) {
return;
}
new Builder(mApp).setDuration(Toast.LENGTH_SHORT).setFill(false).setGravity(Gravity.CENTER).setOffset(0).setreplacedle(notice).setTextColor(Color.WHITE).setBackgroundColor(background).build().show();
}
public static void showRoundRectToast(CharSequence notice, CharSequence desc) {
checkMainThread();
checkContext();
if (TextUtils.isEmpty(notice)) {
return;
}
new Builder(mApp).setDuration(Toast.LENGTH_SHORT).setFill(false).setGravity(Gravity.CENTER).setOffset(0).setDesc(desc).setreplacedle(notice).setTextColor(Color.WHITE).setBackgroundColor(background).build().show();
}
public static void showRoundRectToast(@LayoutRes int layout) {
checkMainThread();
checkContext();
if (layout == 0) {
return;
}
new Builder(mApp).setDuration(Toast.LENGTH_SHORT).setFill(false).setGravity(Gravity.CENTER).setOffset(0).setLayout(layout).build().show();
}
public final static clreplaced Builder {
private Context context;
private CharSequence replacedle;
private CharSequence desc;
private int gravity = Gravity.TOP;
private boolean isFill;
private int yOffset;
private int duration = Toast.LENGTH_SHORT;
private int textColor = Color.WHITE;
private int backgroundColor = R.drawable.shape_toast_bg_r10;
private int layout;
public Builder(Context context) {
this.context = context;
}
public Builder setreplacedle(CharSequence replacedle) {
this.replacedle = replacedle;
return this;
}
public Builder setDesc(CharSequence desc) {
this.desc = desc;
return this;
}
public Builder setGravity(int gravity) {
this.gravity = gravity;
return this;
}
public Builder setFill(boolean fill) {
isFill = fill;
return this;
}
public Builder setOffset(int yOffset) {
this.yOffset = yOffset;
return this;
}
public Builder setDuration(int duration) {
this.duration = duration;
return this;
}
public Builder setTextColor(int textColor) {
this.textColor = textColor;
return this;
}
public Builder setBackgroundColor(int backgroundColor) {
this.backgroundColor = backgroundColor;
return this;
}
public Builder setLayout(@LayoutRes int layout) {
this.layout = layout;
return this;
}
public Toast build() {
if (!checkNull(mToast)) {
mToast.get().cancel();
}
Toast toast = new Toast(context);
HookToast.hook(toast);
if (isFill) {
toast.setGravity(gravity | Gravity.FILL_HORIZONTAL, 0, yOffset);
} else {
toast.setGravity(gravity, 0, yOffset);
}
toast.setDuration(duration);
toast.setMargin(0, 0);
if (layout == 0) {
FrameLayout rootView = (FrameLayout) LayoutInflater.from(context).inflate(R.layout.view_toast_custom, null);
TextView textView = rootView.findViewById(R.id.toastTextView);
TextView descTv = rootView.findViewById(R.id.desc);
rootView.setBackgroundResource(backgroundColor);
textView.setTextColor(textColor);
textView.setText(replacedle);
if (TextUtils.isEmpty(desc)) {
descTv.setVisibility(View.GONE);
} else {
descTv.setText(desc);
descTv.setVisibility(View.VISIBLE);
}
toast.setView(rootView);
} else {
View view = LayoutInflater.from(context).inflate(layout, null);
toast.setView(view);
}
mToast = new SoftReference<>(toast);
return toast;
}
}
private static boolean checkNull(SoftReference softReference) {
if (softReference == null || softReference.get() == null) {
return true;
}
return false;
}
private static void checkMainThread() {
if (!X5WebUtils.isMainThread()) {
throw new IllegalStateException("请不要在子线程中做弹窗操作");
}
}
/**
* <pre>
* @author yangchong
* email : [email protected]
* time : 20120/5/6
* desc : 利用hook解决toast崩溃问题
* revise:
* </pre>
*/
public static clreplaced HookToast {
private static Field sField_TN;
private static Field sField_TN_Handler;
static {
try {
Clreplaced<?> clazz = Toast.clreplaced;
sField_TN = clazz.getDeclaredField("mTN");
sField_TN.setAccessible(true);
sField_TN_Handler = sField_TN.getType().getDeclaredField("mHandler");
sField_TN_Handler.setAccessible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void hook(Toast toast) {
try {
Object tn = sField_TN.get(toast);
Handler preHandler = (Handler) sField_TN_Handler.get(tn);
sField_TN_Handler.set(tn, new HookToast.SafelyHandler(preHandler));
} catch (Exception e) {
e.printStackTrace();
}
}
public static clreplaced SafelyHandler extends Handler {
private Handler impl;
public SafelyHandler(Handler impl) {
this.impl = impl;
}
public void dispatchMessage(Message msg) {
try {
super.dispatchMessage(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
public void handleMessage(Message msg) {
// 需要委托给原Handler执行
impl.handleMessage(msg);
}
}
}
}
19
Source : ToastUtils.java
with Apache License 2.0
from yangchong211
with Apache License 2.0
from yangchong211
/**
* 初始化吐司工具类
* @param app 应用
*/
public static void init(@NonNull final Application app) {
mApp = app;
}
19
Source : MusicSpUtils.java
with Apache License 2.0
from yangchong211
with Apache License 2.0
from yangchong211
public static void init(Application application) {
context = application;
}
19
Source : ScreenDensityUtils.java
with Apache License 2.0
from yangchong211
with Apache License 2.0
from yangchong211
/**
* 在 application 中全局激活适配(也可单独使用 match() 方法在指定页面中配置适配)
*/
@RequiresApi(api = Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public static void register(@NonNull final Application application, final float designSize, final int matchBase, final int matchUnit) {
if (mActivityLifecycleCallback == null) {
mActivityLifecycleCallback = new Application.ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
if (activity != null) {
match(activity, designSize, matchBase, matchUnit);
}
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityResumed(Activity activity) {
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
@Override
public void onActivityDestroyed(Activity activity) {
}
};
application.registerActivityLifecycleCallbacks(mActivityLifecycleCallback);
}
}
19
Source : ScreenDensityUtils.java
with Apache License 2.0
from yangchong211
with Apache License 2.0
from yangchong211
/**
* 全局取消所有的适配
*/
@RequiresApi(api = Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public static void unregister(@NonNull final Application application, @NonNull int... matchUnit) {
if (mActivityLifecycleCallback != null) {
application.unregisterActivityLifecycleCallbacks(mActivityLifecycleCallback);
mActivityLifecycleCallback = null;
}
for (int unit : matchUnit) {
cancelMatch(application, unit);
}
}
19
Source : BaseApp.java
with Apache License 2.0
from yangchong211
with Apache License 2.0
from yangchong211
public clreplaced BaseApp extends Application {
private static Application application;
public static Application getApp() {
if (application == null) {
synchronized (BaseApp.clreplaced) {
if (application == null) {
application = new Application();
}
}
}
return application;
}
@Override
public void onCreate() {
super.onCreate();
application = this;
}
}
See More Examples