|
|
@@ -1,8 +1,12 @@
|
|
|
package com.uas.kesai_esop.activity;
|
|
|
|
|
|
+import android.content.ActivityNotFoundException;
|
|
|
import android.content.Context;
|
|
|
import android.content.Intent;
|
|
|
+import android.content.pm.ActivityInfo;
|
|
|
+import android.content.res.Configuration;
|
|
|
import android.graphics.Color;
|
|
|
+import android.net.Uri;
|
|
|
import android.os.Build;
|
|
|
import android.os.Bundle;
|
|
|
import android.os.Environment;
|
|
|
@@ -15,8 +19,10 @@ import android.view.WindowManager;
|
|
|
import android.widget.ImageView;
|
|
|
import android.widget.LinearLayout;
|
|
|
import android.widget.TextView;
|
|
|
+import android.widget.Toast;
|
|
|
|
|
|
import com.github.barteksc.pdfviewer.PDFView;
|
|
|
+import com.github.barteksc.pdfviewer.util.FitPolicy;
|
|
|
import com.uas.kesai_esop.R;
|
|
|
import com.uas.kesai_esop.util.CommonUtil;
|
|
|
import com.uas.kesai_esop.util.DateFormatUtil;
|
|
|
@@ -44,7 +50,7 @@ public class PdfsActivity2 extends AppCompatActivity {
|
|
|
private boolean useThemeStatusBarColor = false;//是否使用特殊的标题栏背景颜色,android5.0以上可以设置状态栏背景色,如果不使用则使用透明色值
|
|
|
private boolean useStatusBarColor = true;//是否使用状态栏文字和图标为暗色,如果状态栏采用了白色系,则需要使状态栏和图标为暗色,android6.0以上可以设置
|
|
|
private int option;
|
|
|
- private boolean fullscreen=false;
|
|
|
+ private boolean fullscreen = false;
|
|
|
private Context context;
|
|
|
private String destDirPath;
|
|
|
private String filename;
|
|
|
@@ -52,6 +58,7 @@ public class PdfsActivity2 extends AppCompatActivity {
|
|
|
private PDFView pdfView;
|
|
|
private String filenametime;
|
|
|
private int rotate = -90;
|
|
|
+ private String pdffilePath;
|
|
|
|
|
|
@Override
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
@@ -59,11 +66,11 @@ public class PdfsActivity2 extends AppCompatActivity {
|
|
|
setContentView(R.layout.act_pdf);
|
|
|
|
|
|
window = getWindow();
|
|
|
- context=this;
|
|
|
+ context = this;
|
|
|
Intent intent = getIntent();
|
|
|
ArrayList<String> lists = intent.getStringArrayListExtra("lists");
|
|
|
filename = intent.getStringExtra("filename");
|
|
|
- for (int i=0;i<lists.size();i++){
|
|
|
+ for (int i = 0; i < lists.size(); i++) {
|
|
|
filepath = lists.get(i);
|
|
|
Log.e("smd5", filepath);
|
|
|
}
|
|
|
@@ -79,20 +86,29 @@ public class PdfsActivity2 extends AppCompatActivity {
|
|
|
iv_back.setColorFilter(this.getResources().getColor(R.color.zise));
|
|
|
iv_fullscreen.setColorFilter(this.getResources().getColor(R.color.zise));
|
|
|
|
|
|
-// WebView webView = findViewById(R.id.webView);
|
|
|
-// WebSettings webSettings = webView.getSettings();
|
|
|
-// webSettings.setJavaScriptEnabled(true);
|
|
|
-// webSettings.setPluginState(WebSettings.PluginState.ON);
|
|
|
-// webSettings.setAllowContentAccess(true);
|
|
|
-// webSettings.setAllowFileAccess(true);
|
|
|
-// webSettings.setAllowFileAccessFromFileURLs(true);
|
|
|
-// webSettings.setAllowUniversalAccessFromFileURLs(true);
|
|
|
-//// webView.loadUrl("file:///android_asset/web/viewer.html?file="+filepath);
|
|
|
-//// webView.loadUrl("file:///android_asset/web/viewer.html?file="+filepath);
|
|
|
-// webView.loadUrl("http://docs.google.com/gview?embedded=true&url=" + filepath);
|
|
|
-// webView.getSettings().setBuiltInZoomControls(true);
|
|
|
-// webView.getSettings().setSupportZoom(true);
|
|
|
-// webView.getSettings().setDisplayZoomControls(false);//不显示缩放按钮
|
|
|
+ int screen = getResources().getConfiguration().orientation;
|
|
|
+ if (screen == Configuration.ORIENTATION_LANDSCAPE) { //横屏
|
|
|
+ Log.e("aaa", "当前是横屏");
|
|
|
+ }else if (screen == Configuration.ORIENTATION_PORTRAIT) { //竖屏
|
|
|
+ Log.e("aaa", "当前是竖屏");
|
|
|
+ }
|
|
|
+
|
|
|
+ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); //切竖屏
|
|
|
+
|
|
|
+ // WebView webView = findViewById(R.id.webView);
|
|
|
+ // WebSettings webSettings = webView.getSettings();
|
|
|
+ // webSettings.setJavaScriptEnabled(true);
|
|
|
+ // webSettings.setPluginState(WebSettings.PluginState.ON);
|
|
|
+ // webSettings.setAllowContentAccess(true);
|
|
|
+ // webSettings.setAllowFileAccess(true);
|
|
|
+ // webSettings.setAllowFileAccessFromFileURLs(true);
|
|
|
+ // webSettings.setAllowUniversalAccessFromFileURLs(true);
|
|
|
+ //// webView.loadUrl("file:///android_asset/web/viewer.html?file="+filepath);
|
|
|
+ //// webView.loadUrl("file:///android_asset/web/viewer.html?file="+filepath);
|
|
|
+ // webView.loadUrl("http://docs.google.com/gview?embedded=true&url=" + filepath);
|
|
|
+ // webView.getSettings().setBuiltInZoomControls(true);
|
|
|
+ // webView.getSettings().setSupportZoom(true);
|
|
|
+ // webView.getSettings().setDisplayZoomControls(false);//不显示缩放按钮
|
|
|
|
|
|
iv_goback.setOnClickListener(new View.OnClickListener() {
|
|
|
@Override
|
|
|
@@ -104,27 +120,35 @@ public class PdfsActivity2 extends AppCompatActivity {
|
|
|
iv_fullscreen.setOnClickListener(new View.OnClickListener() {
|
|
|
@Override
|
|
|
public void onClick(View v) {//全屏
|
|
|
- if (fullscreen==false){
|
|
|
- fullscreen=true;
|
|
|
+ if (fullscreen == false) {
|
|
|
+ fullscreen = true;
|
|
|
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
|
|
iv_fullscreen.setImageResource(R.drawable.not_fullscreen);
|
|
|
- }else {
|
|
|
- fullscreen=false;
|
|
|
+ } else {
|
|
|
+ fullscreen = false;
|
|
|
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
|
|
iv_fullscreen.setImageResource(R.drawable.fullscreen);
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
});
|
|
|
|
|
|
tv_rotate.setOnClickListener(new View.OnClickListener() {
|
|
|
@Override
|
|
|
public void onClick(View v) {
|
|
|
- rotate -= 90;
|
|
|
- if (rotate <= -360) {
|
|
|
- rotate = -90;
|
|
|
+// rotate -= 90;
|
|
|
+// if (rotate <= -360) {
|
|
|
+// rotate = -90;
|
|
|
+// }
|
|
|
+// pdfView.setRotation(rotate);
|
|
|
+
|
|
|
+ int screen = getResources().getConfiguration().orientation;
|
|
|
+ if (screen == Configuration.ORIENTATION_LANDSCAPE) { //横屏
|
|
|
+ Log.e("aaa", "当前是竖屏");
|
|
|
+ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); //切竖屏
|
|
|
+ }else if (screen == Configuration.ORIENTATION_PORTRAIT) { //竖屏
|
|
|
+ Log.e("aaa", "当前是横屏");
|
|
|
+ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); //切横屏
|
|
|
}
|
|
|
- pdfView.setRotation(rotate);
|
|
|
}
|
|
|
});
|
|
|
|
|
|
@@ -132,9 +156,9 @@ public class PdfsActivity2 extends AppCompatActivity {
|
|
|
@Override
|
|
|
public void run() {
|
|
|
boolean pathExist = isPathExist(destDirPath);
|
|
|
- if (pathExist==true){
|
|
|
+ if (pathExist == true) {
|
|
|
downloadFile1(filepath, destDirPath);
|
|
|
- }else {
|
|
|
+ } else {
|
|
|
try {
|
|
|
CreateText(filepath, destDirPath);
|
|
|
} catch (IOException e) {
|
|
|
@@ -145,6 +169,7 @@ public class PdfsActivity2 extends AppCompatActivity {
|
|
|
}).start();
|
|
|
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 判断路径是否存在
|
|
|
*
|
|
|
@@ -158,24 +183,25 @@ public class PdfsActivity2 extends AppCompatActivity {
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
+
|
|
|
//创建文件夹及文件
|
|
|
- public void CreateText(String filepath,String destDirPath) throws IOException {
|
|
|
+ public void CreateText(String filepath, String destDirPath) throws IOException {
|
|
|
File file = new File(destDirPath);
|
|
|
if (!file.exists()) {
|
|
|
try {
|
|
|
//按照指定的路径创建文件夹
|
|
|
file.mkdirs();
|
|
|
- downloadFile1(filepath,destDirPath);
|
|
|
+ downloadFile1(filepath, destDirPath);
|
|
|
} catch (Exception e) {
|
|
|
// TODO: handle exception
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
- public void downloadFile1(String urlPath,String path) {
|
|
|
- try{
|
|
|
+
|
|
|
+ public void downloadFile1(String urlPath, String path) {
|
|
|
+ try {
|
|
|
final long startTime = System.currentTimeMillis();
|
|
|
- Log.i("DOWNLOAD","startTime="+startTime);
|
|
|
+ Log.i("DOWNLOAD", "startTime=" + startTime);
|
|
|
//下载函数
|
|
|
filenametime = DateFormatUtil.getProofTime();
|
|
|
//获取文件名
|
|
|
@@ -184,34 +210,35 @@ public class PdfsActivity2 extends AppCompatActivity {
|
|
|
conn.connect();
|
|
|
InputStream is = conn.getInputStream();
|
|
|
int fileSize = conn.getContentLength();//根据响应获取文件大小
|
|
|
- if (fileSize <= 0) throw new RuntimeException("无法获知文件大小 ");
|
|
|
- if (is == null) throw new RuntimeException("stream is null");
|
|
|
+ if (fileSize <= 0)
|
|
|
+ throw new RuntimeException("无法获知文件大小 ");
|
|
|
+ if (is == null)
|
|
|
+ throw new RuntimeException("stream is null");
|
|
|
File file1 = new File(path);
|
|
|
- if(!file1.exists()){
|
|
|
+ if (!file1.exists()) {
|
|
|
file1.mkdirs();
|
|
|
}
|
|
|
//把数据存入路径+文件名
|
|
|
- FileOutputStream fos = new FileOutputStream(path+"/"+ filenametime +filename);
|
|
|
+ FileOutputStream fos = new FileOutputStream(path + "/" + filenametime + filename);
|
|
|
byte buf[] = new byte[1024];
|
|
|
int downLoadFileSize = 0;
|
|
|
- do{
|
|
|
+ do {
|
|
|
//循环读取
|
|
|
int numread = is.read(buf);
|
|
|
- if (numread == -1)
|
|
|
- {
|
|
|
+ if (numread == -1) {
|
|
|
break;
|
|
|
}
|
|
|
fos.write(buf, 0, numread);
|
|
|
downLoadFileSize += numread;
|
|
|
//更新进度条
|
|
|
} while (true);
|
|
|
- Log.i("DOWNLOAD","download success");
|
|
|
- Log.i("DOWNLOAD","totalTime="+ (System.currentTimeMillis() - startTime));
|
|
|
- Log.i("DOWNLOAD","文件保存到:"+path);
|
|
|
+ Log.i("DOWNLOAD", "download success");
|
|
|
+ Log.i("DOWNLOAD", "totalTime=" + (System.currentTimeMillis() - startTime));
|
|
|
+ Log.i("DOWNLOAD", "文件保存到:" + path);
|
|
|
Message message = handler.obtainMessage();
|
|
|
message.what = 1;
|
|
|
- message.obj = path+"/"+ filenametime +filename;
|
|
|
-// handler.sendEmptyMessage(0);
|
|
|
+ message.obj = path + "/" + filenametime + filename;
|
|
|
+ // handler.sendEmptyMessage(0);
|
|
|
handler.sendMessage(message);
|
|
|
is.close();
|
|
|
} catch (Exception ex) {
|
|
|
@@ -219,7 +246,6 @@ public class PdfsActivity2 extends AppCompatActivity {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
private Handler handler = new Handler() {
|
|
|
|
|
|
@Override
|
|
|
@@ -227,24 +253,19 @@ public class PdfsActivity2 extends AppCompatActivity {
|
|
|
//CommonUtil.toastNoRepeat(context,"文件保存到"+destDirPath);
|
|
|
switch (msg.what) {
|
|
|
case 1:
|
|
|
- String pdffilePath = (String) msg.obj;
|
|
|
+ pdffilePath = (String) msg.obj;
|
|
|
+ Log.e("aaa", "PDF文件路径是:" + pdffilePath);
|
|
|
getPdf(pdffilePath);
|
|
|
|
|
|
-// pdfView.fromAsset(pdffilePath)
|
|
|
-// .enableSwipe(true) // 允许滑动翻页
|
|
|
-// .swipeHorizontal(false) // 垂直翻页
|
|
|
-// .enableDoubletap(true) // 允许双击缩放
|
|
|
-// .defaultPage(0) // 默认显示第一页
|
|
|
-// .load();
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 预览本地 pdf 文件
|
|
|
+ *
|
|
|
* @param filePath 本地文件完整路径
|
|
|
*/
|
|
|
private void getPdf(String filePath) {
|
|
|
@@ -254,11 +275,48 @@ public class PdfsActivity2 extends AppCompatActivity {
|
|
|
try {
|
|
|
InputStream in = new FileInputStream(file);
|
|
|
if (in != null) {
|
|
|
+ // Class<?> clazz = pdfView.getClass();
|
|
|
+ // Method method = clazz.getDeclaredMethod("setPageFitPolicy", FitPolicy.class);
|
|
|
+ // method.setAccessible(true); // 设置为true以访问私有方法
|
|
|
+ // method.invoke(pdfView, FitPolicy.HEIGHT); // 传递包装对象而非原始值
|
|
|
+ // Log.e("aaa", "反射的值是:" + pdfView.getPageFitPolicy());
|
|
|
+
|
|
|
+ pdfView.zoomTo(50);
|
|
|
+ pdfView.setSwipeEnabled(false);
|
|
|
+
|
|
|
pdfView.fromStream(in)
|
|
|
- .enableAnnotationRendering(true) //可以显示电子章
|
|
|
+ // .enableAnnotationRendering(true) //可以显示电子章
|
|
|
+ .pages(0, 2, 1) // 加载第0, 2, 1页(从0开始计数
|
|
|
+ .enableDoubletap(true) //启用双击缩放
|
|
|
+ .enableAnnotationRendering(true) // 启用注释渲染
|
|
|
+// .swipeHorizontal(false) //启用水平滑动
|
|
|
+ .swipeHorizontal(true) //启用垂直滑动
|
|
|
+ .pageFitPolicy(FitPolicy.BOTH) //修改策略模式,完全展示
|
|
|
+ // .onPageChange(new OnPageChangeListener() {
|
|
|
+ // @Override
|
|
|
+ // public void onPageChanged(int page, int pageCount) {
|
|
|
+ // Log.e("aaa", "滑动页数:" + page);
|
|
|
+ // Log.e("aaa", "滑动页数总数:" + pageCount);
|
|
|
+ // // pdfView.fitToWidth(0);
|
|
|
+ // }
|
|
|
+ // })
|
|
|
+
|
|
|
+// .onRender(new OnRenderListener() { //pdf文档加载完成后才能使用
|
|
|
+// @Override
|
|
|
+// public void onInitiallyRendered(int nbPages) {
|
|
|
+// Log.e("aaa", "初始加载页数:" + nbPages);
|
|
|
+// pdfView.fitToWidth(nbPages-1);
|
|
|
+// }
|
|
|
+// })
|
|
|
+ .defaultPage(0)
|
|
|
.load();
|
|
|
}
|
|
|
|
|
|
+ //旋转90度
|
|
|
+// pdfView.setRotation(-90);
|
|
|
+
|
|
|
+ //openPDFWithSystemViewer(PdfsActivity2.this,pdffilePath);
|
|
|
+
|
|
|
// pdfView.fromAsset(pdffilePath)
|
|
|
// .enableSwipe(true) // 允许滑动翻页
|
|
|
// .swipeHorizontal(false) // 垂直翻页
|
|
|
@@ -270,7 +328,19 @@ public class PdfsActivity2 extends AppCompatActivity {
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
- CommonUtil.toastNoRepeat(context,"文件不存在");
|
|
|
+ CommonUtil.toastNoRepeat(context, "文件不存在");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 使用系统PDF Viewer打开PDF
|
|
|
+ public void openPDFWithSystemViewer(Context context, String pdfPath) {
|
|
|
+ Intent intent = new Intent(Intent.ACTION_VIEW);
|
|
|
+ intent.setDataAndType(Uri.parse("file://" + pdfPath), "application/pdf");
|
|
|
+ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
+ try {
|
|
|
+ context.startActivity(intent);
|
|
|
+ } catch (ActivityNotFoundException e) {
|
|
|
+ Toast.makeText(context, "请确保安装了可以处理PDF文件的应用。", Toast.LENGTH_LONG).show();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -295,31 +365,34 @@ public class PdfsActivity2 extends AppCompatActivity {
|
|
|
}
|
|
|
setStatusTextColor(true);
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 设置状态栏文字色值为深色调
|
|
|
+ *
|
|
|
* @param useDart 是否使用深色调
|
|
|
*/
|
|
|
|
|
|
private void setStatusTextColor(Boolean useDart) {
|
|
|
if (useDart) {
|
|
|
- if (isFlyme()){
|
|
|
+ if (isFlyme()) {
|
|
|
processFlyMe(useDart);
|
|
|
- }else if (isMIUI()){
|
|
|
+ } else if (isMIUI()) {
|
|
|
processMIUI(useDart);
|
|
|
- }else {
|
|
|
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
|
|
|
+ } else {
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
|
|
|
- }else {
|
|
|
+ } else {
|
|
|
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);//实现状态栏图标和文字颜色为暗色
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
private void processFlyMe(Boolean isLightStatusBar) {
|
|
|
WindowManager.LayoutParams lp = window.getAttributes();
|
|
|
try {
|
|
|
-// Class instance = getClass().forName("android.view.WindowManager\$LayoutParams");
|
|
|
+ // Class instance = getClass().forName("android.view.WindowManager\$LayoutParams");
|
|
|
Class instance = Class.forName("android.view.WindowManager$LayoutParams");
|
|
|
int value = instance.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON").getInt(lp);
|
|
|
Field field = instance.getDeclaredField("meizuFlags");
|
|
|
@@ -334,21 +407,22 @@ public class PdfsActivity2 extends AppCompatActivity {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 改变小米的状态栏字体颜色为黑色, 要求MIUI6以上 lightStatusBar为真时表示黑色字体
|
|
|
*/
|
|
|
private void processMIUI(Boolean lightStatusBar) {
|
|
|
Class clazz = window.getClass();
|
|
|
try {
|
|
|
- int darkModeFlag;
|
|
|
-// val layoutParams = Class.forName("android.view.MiuiWindowManager\$LayoutParams")
|
|
|
+ int darkModeFlag;
|
|
|
+ // val layoutParams = Class.forName("android.view.MiuiWindowManager\$LayoutParams")
|
|
|
Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
|
|
|
Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
|
|
|
darkModeFlag = field.getInt(layoutParams);
|
|
|
-// val extraFlagField = clazz.getMethod("setExtraFlags", Int::class.javaPrimitiveType, Int::class.javaPrimitiveType)
|
|
|
-// extraFlagField.invoke(window, if (lightStatusBar) darkModeFlag else 0, darkModeFlag)
|
|
|
+ // val extraFlagField = clazz.getMethod("setExtraFlags", Int::class.javaPrimitiveType, Int::class.javaPrimitiveType)
|
|
|
+ // extraFlagField.invoke(window, if (lightStatusBar) darkModeFlag else 0, darkModeFlag)
|
|
|
Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
|
|
|
- extraFlagField.invoke(window,lightStatusBar?darkModeFlag :0,darkModeFlag);
|
|
|
+ extraFlagField.invoke(window, lightStatusBar ? darkModeFlag : 0, darkModeFlag);
|
|
|
} catch (Exception ignored) {
|
|
|
ignored.printStackTrace();
|
|
|
}
|