parent
0cf2304b22
commit
b22541db74
@ -1,173 +0,0 @@ |
||||
package xyz.fycz.myreader.ui.dialog; |
||||
|
||||
|
||||
import android.annotation.SuppressLint; |
||||
import android.content.Intent; |
||||
import android.net.Uri; |
||||
import android.os.Handler; |
||||
import android.os.Message; |
||||
import android.view.View; |
||||
import androidx.appcompat.app.AppCompatActivity; |
||||
|
||||
import xyz.fycz.myreader.application.App; |
||||
import xyz.fycz.myreader.webapi.LanZousApi; |
||||
import xyz.fycz.myreader.webapi.ResultCallback; |
||||
import xyz.fycz.myreader.common.APPCONST; |
||||
import xyz.fycz.myreader.ui.activity.MainActivity; |
||||
import xyz.fycz.myreader.ui.fragment.BookcaseFragment; |
||||
import xyz.fycz.myreader.util.IOUtils; |
||||
import xyz.fycz.myreader.util.ToastUtils; |
||||
import xyz.fycz.myreader.util.utils.FileUtils; |
||||
|
||||
import java.io.File; |
||||
import java.io.FileOutputStream; |
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
import java.net.HttpURLConnection; |
||||
import java.net.URL; |
||||
|
||||
/** |
||||
* @author fengyue |
||||
* @date 2020/5/20 20:50 |
||||
*/ |
||||
|
||||
public class APPDownloadTip { |
||||
|
||||
private String url; |
||||
private BookcaseFragment mBookcaseFragment; |
||||
private MainActivity activity; |
||||
private boolean isForceUpdate; |
||||
|
||||
public APPDownloadTip(String url, BookcaseFragment mBookcaseFragment, AppCompatActivity activity, boolean isForceUpdate) { |
||||
this.url = url; |
||||
this.mBookcaseFragment = mBookcaseFragment; |
||||
this.activity = (MainActivity) activity; |
||||
this.isForceUpdate = isForceUpdate; |
||||
} |
||||
|
||||
@SuppressLint("HandlerLeak") |
||||
private Handler mHandler = new Handler() { |
||||
@Override |
||||
public void handleMessage(Message msg) { |
||||
if (!App.isDestroy(activity)) { |
||||
switch (msg.what) { |
||||
case 1: |
||||
mBookcaseFragment.getTvDownloadTip().setText("获取下载链接失败,请前往浏览器下载!"); |
||||
mBookcaseFragment.getRlDownloadTip().setVisibility(View.GONE); |
||||
break; |
||||
case 2: |
||||
mBookcaseFragment.getTvDownloadTip().setText("连接中..."); |
||||
break; |
||||
case 3: |
||||
updateDownloadPro((double) msg.obj); |
||||
break; |
||||
case 4: |
||||
mBookcaseFragment.getRlDownloadTip().setVisibility(View.GONE); |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
}; |
||||
|
||||
public void downloadApp() { |
||||
mBookcaseFragment.getTvStopDownload().setVisibility(View.GONE); |
||||
mBookcaseFragment.getRlDownloadTip().setVisibility(View.VISIBLE); |
||||
mBookcaseFragment.getPbDownload().setProgress(0); |
||||
mBookcaseFragment.getTvDownloadTip().setText("正在获取下载链接..."); |
||||
LanZousApi.getUrl(url, new ResultCallback() { |
||||
@Override |
||||
public void onFinish(Object o, int code) { |
||||
final String downloadUrl = (String) o; |
||||
if (downloadUrl == null) { |
||||
error(); |
||||
return; |
||||
} |
||||
App.getApplication().newThread(() -> { |
||||
HttpURLConnection con = null; |
||||
InputStream is = null; |
||||
FileOutputStream fos = null; |
||||
File appFile = null; |
||||
try { |
||||
URL webUrl = new URL(downloadUrl); |
||||
mHandler.sendMessage(mHandler.obtainMessage(2)); |
||||
con = (HttpURLConnection) webUrl.openConnection(); |
||||
is = con.getInputStream(); |
||||
String filePath = APPCONST.UPDATE_APK_FILE_DIR + "FYReader.apk.temp"; |
||||
appFile = FileUtils.getFile(filePath); |
||||
fos = new FileOutputStream(appFile); |
||||
byte[] tem = new byte[1024]; |
||||
long alreadyLen = 0; |
||||
long fileLength = con.getContentLength(); |
||||
int len; |
||||
double progress; |
||||
while ((len = is.read(tem)) != -1) { |
||||
fos.write(tem, 0, len); |
||||
alreadyLen += len; |
||||
progress = alreadyLen * 1.0f * 100f / fileLength; |
||||
mHandler.sendMessage(mHandler.obtainMessage(3, progress)); |
||||
} |
||||
fos.flush(); |
||||
if (fileLength == appFile.length()) { |
||||
String newPath = filePath.replace(".temp", ""); |
||||
final File newFile = new File(newPath); |
||||
if (appFile.renameTo(newFile)) { |
||||
mHandler.sendMessage(mHandler.obtainMessage(4)); |
||||
DialogCreator.createCommonDialog(activity, "提示", "风月读书下载完成,安装包路径:" + newPath, |
||||
!isForceUpdate, "取消", "立即安装", (dialog, which) -> { |
||||
if (isForceUpdate) { |
||||
activity.finish(); |
||||
} |
||||
}, (dialog, which) -> activity.installProcess(newFile, isForceUpdate)); |
||||
activity.installProcess(newFile, isForceUpdate); |
||||
} else { |
||||
appFile.delete(); |
||||
error(); |
||||
} |
||||
} else { |
||||
appFile.delete(); |
||||
error(); |
||||
} |
||||
} catch (IOException e) { |
||||
if (appFile != null) { |
||||
appFile.delete(); |
||||
} |
||||
error(); |
||||
e.printStackTrace(); |
||||
} finally { |
||||
if (con != null) { |
||||
con.disconnect(); |
||||
} |
||||
IOUtils.close(is, fos); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
@Override |
||||
public void onError(Exception e) { |
||||
error(); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
|
||||
private void error() { |
||||
mHandler.sendMessage(mHandler.obtainMessage(1)); |
||||
ToastUtils.showError("获取下载链接失败,请前往浏览器下载!"); |
||||
Intent intent = new Intent(); |
||||
intent.setAction(Intent.ACTION_VIEW); |
||||
intent.setData(Uri.parse(url)); |
||||
activity.startActivity(intent); |
||||
if (isForceUpdate) { |
||||
activity.finish(); |
||||
} |
||||
} |
||||
|
||||
@SuppressLint({"SetTextI18n"}) |
||||
private void updateDownloadPro(double progress) { |
||||
mBookcaseFragment.getPbDownload().setProgress((int) progress); |
||||
//保留两位小数
|
||||
//mBookcaseFragment.getTvDownloadTip().setText("正在下载风月读书最新版本...[" + String.format("%.2f", progress) + "%]");
|
||||
mBookcaseFragment.getTvDownloadTip().setText("正在下载风月读书最新版本...[" + (int) progress + "%]"); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,64 @@ |
||||
package xyz.fycz.myreader.widget; |
||||
|
||||
import android.content.Context; |
||||
import android.util.AttributeSet; |
||||
import android.view.MotionEvent; |
||||
|
||||
import androidx.viewpager.widget.ViewPager; |
||||
|
||||
/** |
||||
* @author fengyue |
||||
* @date 2021/6/2 19:57 |
||||
*/ |
||||
//禁止左右滑动的viewpager
|
||||
public class NoScrollViewPager extends ViewPager { |
||||
|
||||
private boolean enableScroll = true; |
||||
|
||||
public NoScrollViewPager(Context context, AttributeSet attrs) { |
||||
super(context, attrs); |
||||
} |
||||
|
||||
public NoScrollViewPager(Context context) { |
||||
super(context); |
||||
} |
||||
|
||||
//调用此方法 参数为false 即可禁止滑动
|
||||
public void setEnableScroll(boolean noScroll) { |
||||
this.enableScroll = noScroll; |
||||
} |
||||
|
||||
@Override |
||||
public void scrollTo(int x, int y) { |
||||
// if(noScroll){ //加上判断无法用 setCurrentItem 方法切换
|
||||
super.scrollTo(x, y); |
||||
// }
|
||||
} |
||||
|
||||
@Override |
||||
public boolean onTouchEvent(MotionEvent arg0) { |
||||
if (!enableScroll) |
||||
return false; |
||||
else |
||||
return super.onTouchEvent(arg0); |
||||
} |
||||
|
||||
@Override |
||||
public boolean onInterceptTouchEvent(MotionEvent arg0) { |
||||
if (!enableScroll) |
||||
return false; |
||||
else |
||||
return super.onInterceptTouchEvent(arg0); |
||||
} |
||||
|
||||
@Override |
||||
public void setCurrentItem(int item, boolean smoothScroll) { |
||||
super.setCurrentItem(item, smoothScroll); |
||||
} |
||||
|
||||
@Override |
||||
public void setCurrentItem(int item) { |
||||
super.setCurrentItem(item); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,95 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:app="http://schemas.android.com/apk/res-auto" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent"> |
||||
|
||||
<LinearLayout |
||||
android:id="@+id/ll_no_data_tips" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_centerInParent="true" |
||||
android:gravity="center" |
||||
android:orientation="vertical" |
||||
android:padding="10dp" |
||||
android:visibility="gone"> |
||||
|
||||
<ImageView |
||||
android:layout_width="100dp" |
||||
android:layout_height="100dp" |
||||
app:srcCompat="@drawable/ic_vector_add_bookcase" |
||||
app:tint="@color/textSecondary" /> |
||||
|
||||
<TextView |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_marginTop="10dp" |
||||
android:text="当前无任何书籍,点击添加" |
||||
android:textColor="@color/textSecondary" |
||||
android:textSize="16sp" /> |
||||
</LinearLayout> |
||||
|
||||
<com.scwang.smartrefresh.layout.SmartRefreshLayout |
||||
android:id="@+id/srl_book_list" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent"> |
||||
|
||||
<com.scwang.smartrefresh.header.MaterialHeader |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" /> |
||||
|
||||
<androidx.recyclerview.widget.RecyclerView |
||||
android:id="@+id/rv_book_list" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
android:scrollbars="vertical"/> |
||||
</com.scwang.smartrefresh.layout.SmartRefreshLayout> |
||||
|
||||
<RelativeLayout |
||||
android:id="@+id/rl_book_edit" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="52dp" |
||||
android:layout_alignParentBottom="true" |
||||
android:background="@color/colorForeground" |
||||
android:gravity="center_vertical" |
||||
android:paddingEnd="10dp" |
||||
android:visibility="gone"> |
||||
|
||||
<CheckBox |
||||
android:id="@+id/book_selected_all" |
||||
android:layout_width="120dp" |
||||
android:layout_height="40dp" |
||||
android:layout_centerVertical="true" |
||||
android:layout_marginStart="15dp" |
||||
android:text="全选" |
||||
android:textColor="@color/textSecondary" |
||||
android:textSize="15dp" |
||||
android:theme="@style/MyCheckBox" /> |
||||
|
||||
<Button |
||||
android:id="@+id/book_add_group" |
||||
android:layout_width="90dp" |
||||
android:layout_height="35dp" |
||||
android:layout_alignParentEnd="true" |
||||
android:layout_centerVertical="true" |
||||
android:background="@drawable/selector_btn_add" |
||||
android:clickable="false" |
||||
android:enabled="false" |
||||
android:minWidth="110dp" |
||||
android:text="加入分组" |
||||
android:textColor="@color/selector_btn_file_add" /> |
||||
|
||||
<Button |
||||
android:id="@+id/book_btn_delete" |
||||
android:layout_width="90dp" |
||||
android:layout_height="35dp" |
||||
android:layout_centerVertical="true" |
||||
android:layout_marginEnd="15dp" |
||||
android:layout_toLeftOf="@id/book_add_group" |
||||
android:background="@drawable/selector_btn_add" |
||||
android:clickable="false" |
||||
android:enabled="false" |
||||
android:text="删除/移除" |
||||
android:textColor="@color/selector_btn_file_add" /> |
||||
</RelativeLayout> |
||||
</RelativeLayout> |
Loading…
Reference in new issue