use of io.reactivex.rxjava3.core.ObservableSource in project xxf_android by NBXXF.
the class SystemUtils method saveImageToAlbum.
/**
* 保存图片到相册
* 自动请求权限 没有权限报异常 {@link PermissionDeniedException}
*
* @param context
* @param picName 是name 不说full path
* @param bmp
* @return
*/
public static Observable<File> saveImageToAlbum(FragmentActivity context, String picName, Bitmap bmp) {
return XXF.requestPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE).compose(new RxPermissionTransformer(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)).flatMap(new Function<Boolean, ObservableSource<File>>() {
@Override
public ObservableSource<File> apply(Boolean aBoolean) throws Exception {
return Observable.fromCallable(new Callable<File>() {
@Override
public File call() throws Exception {
// 其次把文件插入到系统图库
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DISPLAY_NAME, picName);
values.put(MediaStore.MediaColumns.MIME_TYPE, com.xxf.utils.FileUtils.getMimeType(picName));
values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DCIM);
ContentResolver contentResolver = context.getContentResolver();
Uri uri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
if (uri == null) {
throw new RuntimeException("图片保存失败");
}
OutputStream fos = contentResolver.openOutputStream(uri);
bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
return new File(UriUtils.getPath(applicationContext, uri));
} else {
File appDir = applicationContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
if (!appDir.exists()) {
appDir.mkdir();
}
// 文件的名称设置为 系统时间.jpg
File file = new File(appDir, picName);
try {
FileOutputStream fos = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
MediaScannerConnection.scanFile(context, new String[] { file.getAbsolutePath() }, new String[] { com.xxf.utils.FileUtils.getMimeType(file.getAbsolutePath()) }, new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String path, Uri uri) {
}
});
// 锤子8.1 必须下面这种扫描方式
MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), picName, null);
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
return file;
}
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
}
});
}
use of io.reactivex.rxjava3.core.ObservableSource in project xxf_android by NBXXF.
the class StateActivity method onCreate.
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<Observable<Boolean>> datas = new ArrayList<>();
datas.add(Observable.fromCallable(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
throw new RuntimeException("xxx");
}
}));
datas.add(Observable.fromCallable(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return true;
}
}));
Observable.concatDelayError(datas).observeOn(AndroidSchedulers.mainThread(), true).doOnError(new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Throwable {
Log.d("=======>data error:", "" + throwable);
}
}).subscribe(new Consumer<Boolean>() {
@Override
public void accept(Boolean aBoolean) throws Throwable {
Log.d("=======>data:", "" + aBoolean);
}
});
stateBinding = ActivityStateBinding.inflate(getLayoutInflater(), null, false);
// stateBinding.grayLayout.setGrayColor(true);
setContentView(stateBinding.getRoot());
// TestViewModel viewModel = XXF.getViewModel(this, TestViewModel.class);
stateBinding.recyclerView.setAdapter(testAdaper = new TestAdaper());
// new ItemTouchHelper(new SimpleItemTouchHelperCallback(testAdaper)).attachToRecyclerView(stateBinding.recyclerView);
// stateBinding.recyclerView.addItemDecoration(new GridItemDecoration(DensityUtil.dip2px(5)));
stateBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
// stateBinding.recyclerView.addItemDecoration(ItemDecorationFactory.createHorizontalItemDecoration(new HorizontalDividerItemDecoration.Builder(this).size(DensityUtil.dip2px(20)).color(Color.RED)));
DividerDecoration dividerItemDecoration = new DividerDecoration(this, Color.YELLOW, DensityUtil.dip2px(20));
stateBinding.recyclerView.addItemDecoration(dividerItemDecoration);
stateBinding.btnTest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// ShareUtil.shareQQ(StateActivity.this, "xxxx");
SystemUtils.shareText(StateActivity.this, "xxxx", null).compose(XXF.bindToErrorNotice()).subscribe();
}
});
stateBinding.btnLoad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new ScrollShotting(stateBinding.recyclerView, 200, Color.WHITE) {
@Override
public void onShot(@NotNull Bitmap bitmap) {
File my_images = new File(getApplication().getExternalFilesDir(Environment.DIRECTORY_PICTURES), "image");
my_images.mkdirs();
File file = new File(new File(getApplication().getExternalFilesDir(Environment.DIRECTORY_PICTURES), "image"), "default_image2.jpg");
boolean b = BitmapUtils.INSTANCE.bitmapToFile(bitmap, file);
ToastUtils.showToast("设置:" + b);
SystemUtils.shareFile(StateActivity.this, file.getAbsolutePath(), FileProvider7.INSTANCE.getAuthority(getApplication()), SystemUtils.SHARE_WEIBO_CIRCLE_COMPONENT).compose(XXF.bindToErrorNotice()).subscribe();
}
}.start();
}
});
testAdaper.bindData(true, new ArrayList<>());
loadData();
stateBinding.btnDownload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/* XXF.getFileService()
.getPrivateFileDir()
.flatMap(new Function<File, ObservableSource<File>>() {
@Override
public ObservableSource<File> apply(File file) throws Throwable {
return XXF.getFileService()
.download("http://vfx.mtime.cn/Video/2019/02/04/mp4/190204084208765161.mp4", new File(file, "test.mp4").getAbsolutePath());
}
})
.compose(XXF.bindToProgressHud())
.subscribe();*/
XXF.getFileService().getFilesDir(false, false).flatMap(new Function<File, Observable<DownloadTask>>() {
@Override
public Observable<DownloadTask> apply(File file) throws Throwable {
return XXF.getFileService().downloadTask("http://vfx.mtime.cn/Video/2019/02/04/mp4/190204084208765161.mp4", new File(file, "test.mp4").getAbsolutePath());
}
}).compose(new ProgressHUDTransformerImpl<DownloadTask>(StateActivity.this) {
@Override
public void onNext(DownloadTask downloadTask) {
super.onNext(downloadTask);
getSafeProgressHUD().updateStateText((downloadTask.getCurrent() * 1.0f / downloadTask.getDuration()) * 100 + "%");
}
}.setDismissOnNext(false)).to(XXF.bindLifecycle(StateActivity.this)).subscribe(new Consumer<DownloadTask>() {
@Override
public void accept(DownloadTask downloadTask) throws Throwable {
Log.d("", "=========>task2:" + downloadTask);
}
});
}
});
}
use of io.reactivex.rxjava3.core.ObservableSource in project xxf_android by NBXXF.
the class MainActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Throwable {
}
});
Observable.defer(new Supplier<ObservableSource<Integer>>() {
@Override
public ObservableSource<Integer> get() throws Throwable {
Log.d("========>测试 开始执行重试:", "" + pageIndex);
return Observable.just(pageIndex).map(new Function<Integer, Integer>() {
@Override
public Integer apply(Integer integer) throws Throwable {
if (integer < 3) {
Log.d("========>测试 异常:", "" + integer);
throw new RuntimeException("");
}
return integer;
}
});
}
}).retry(3, new Predicate<Throwable>() {
@Override
public boolean test(Throwable throwable) throws Throwable {
if (throwable instanceof RuntimeException) {
Log.d("========>测试 重试:", "" + System.currentTimeMillis());
pageIndex += 1;
return true;
}
return false;
}
}).subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Throwable {
Log.d("========>测试 结果:", "" + integer);
}
});
binding = ActivityMainBinding.inflate(getLayoutInflater());
binding.change.setText("normal");
binding.change.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(v.getContext(), NormalRecyclerViewActivity.class).putExtra("xx", "77463"));
}
});
setContentView(binding.getRoot());
System.out.println("==========>onChildViewAttachedToWindow init");
// adapter.setHasStableIds(true);
binding.recyclerView.setAdapter(adapter);
// 删除-> 改变焦点(上一个) 后删除当前
// binding.recyclerView.find
// 创建第五条
binding.recyclerView.setEdgeEffectFactory(new EdgeSpringEffectFactory(0.5f, 0.5f));
binding.recyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
@Override
public void onChildViewAttachedToWindow(@NonNull View view) {
RecyclerView.ViewHolder containingViewHolder = binding.recyclerView.findContainingViewHolder(view);
if (containingViewHolder.getAdapterPosition() == 5) {
// requestfoucs
binding.recyclerView.removeOnChildAttachStateChangeListener(this);
}
System.out.println("==========>onChildViewAttachedToWindow:AdapterPosition:" + containingViewHolder.getAdapterPosition() + " LayoutPosition:" + containingViewHolder.getLayoutPosition() + " hash:" + containingViewHolder.itemView);
}
@Override
public void onChildViewDetachedFromWindow(@NonNull View view) {
}
});
sectionMap.clear();
sectionMap.put(0, "第一个分组");
sectionMap.put(3, "第2个分组");
Paint paint = new Paint();
paint.setTextSize(DensityUtil.sp2px(13));
paint.setColor(Color.RED);
paint.setAntiAlias(true);
Paint background = new Paint();
background.setColor(Color.WHITE);
binding.recyclerView.addItemDecoration(new SectionItemDecoration(new SectionProvider() {
@NotNull
@Override
public TreeMap<Integer, String> onProvideSection() {
return sectionMap;
}
}, paint, background, DensityUtil.sp2px(30), DensityUtil.sp2px(16)));
List<String> list = new ArrayList<>();
int count = new Random().nextInt(50);
for (int i = 0; i < count; i++) {
// list.add("i" + new Random().nextInt(100));
list.add("i" + i);
}
adapter.bindData(true, list);
binding.refresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<String> list = new ArrayList<>();
int count = new Random().nextInt(50);
for (int i = 0; i < count; i++) {
// list.add("i" + new Random().nextInt(100));
list.add("i" + i);
}
adapter.bindData(true, list);
Log.d("=======>list:", "" + list);
}
});
binding.insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.addItem(0, "hello");
}
});
binding.insertLast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.addItem("hello foo");
}
});
binding.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.removeItem(0);
}
});
binding.loadMore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<String> list = new ArrayList<>();
int count = new Random().nextInt(50);
for (int i = 0; i < count; i++) {
// list.add("i" + new Random().nextInt(100));
list.add("i" + i);
}
adapter.addItems(list);
}
});
}
use of io.reactivex.rxjava3.core.ObservableSource in project RxJava by ReactiveX.
the class ObservableRepeatTest method repeatTakeWithSubscribeOn.
@Test
public void repeatTakeWithSubscribeOn() throws InterruptedException {
final AtomicInteger counter = new AtomicInteger();
Observable<Integer> oi = Observable.unsafeCreate(new ObservableSource<Integer>() {
@Override
public void subscribe(Observer<? super Integer> sub) {
sub.onSubscribe(Disposable.empty());
counter.incrementAndGet();
sub.onNext(1);
sub.onNext(2);
sub.onComplete();
}
}).subscribeOn(Schedulers.newThread());
Object[] ys = oi.repeat().subscribeOn(Schedulers.newThread()).map(new Function<Integer, Integer>() {
@Override
public Integer apply(Integer t1) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
return t1;
}
}).take(4).toList().blockingGet().toArray();
assertEquals(2, counter.get());
assertArrayEquals(new Object[] { 1, 2, 1, 2 }, ys);
}
use of io.reactivex.rxjava3.core.ObservableSource in project RxJava by ReactiveX.
the class ObservableRepeatTest method noCancelPreviousRepeatWhen.
@Test
public void noCancelPreviousRepeatWhen() {
final AtomicInteger counter = new AtomicInteger();
Observable<Integer> source = Observable.just(1).doOnDispose(new Action() {
@Override
public void run() throws Exception {
counter.getAndIncrement();
}
});
final AtomicInteger times = new AtomicInteger();
source.repeatWhen(new Function<Observable<Object>, ObservableSource<?>>() {
@Override
public ObservableSource<?> apply(Observable<Object> e) throws Exception {
return e.takeWhile(new Predicate<Object>() {
@Override
public boolean test(Object v) throws Exception {
return times.getAndIncrement() < 4;
}
});
}
}).test().assertResult(1, 1, 1, 1, 1);
assertEquals(0, counter.get());
}
Aggregations