当前位置 : 主页 > 编程语言 > java >

wps工具类

来源:互联网 收集:自由互联 发布时间:2022-06-23
import android.content.Intent; import android.net.Uri; import android.os.Bundle; import java.io.File; import cn.mvp.mlibs.utils.PackageUtils; //import org.xutils.x; /** * Created by Administratoron 2016-08-16. */ public class WpsUtils { //个
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;


import java.io.File;

import cn.mvp.mlibs.utils.PackageUtils;

//import org.xutils.x;


/**
* Created by Administratoron 2016-08-16.
*/
public class WpsUtils {
//个人版的包名
public static final String PACKAGENAME_ENG = "cn.wps.moffice_eng";
//企业版的包名
public static final String PACKAGENAME_ENT = "cn.wps.moffice_ent";
//测试用
public static final String PACKAGENAME = "cn.wps.moffice";
public static final String PACKAGENAME_KING_ENT = "com.kingsoft.moffice_ent";
public static final String PACKAGENAME_KING_PRO = "com.kingsoft.moffice_pro";
//华为定制包名
public static final String PACKAGENAME_KING_PRO_HW = "com.kingsoft.moffice_pro_hw";
//wps类名
public static final String CLASSNAME = "cn.wps.moffice.documentmanager.PreStartActivity2";


/**
* 判断是否是wps能打开的文件
*
* @param file
* @return
*/
public static boolean IsWPSFile(File file) {
String end = file.getName().substring(file.getName().lastIndexOf(".") + 1).toLowerCase();
return end.equals("doc") || end.equals("docx") || end.equals("wps")
|| end.equals("dot") || end.equals("wpt")
|| end.equals("xls") || end.equals("xlsx") || end.equals("et")
|| end.equals("ppt") || end.equals("pptx") || end.equals("dps")
|| end.equals("txt") || end.equals("pdf");
}

public static Intent toOpenOtherFile(File file) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
String type = getMIMEType(file);
intent.setDataAndType(Uri.fromFile(file), type);
return intent;
}

/**
* 识别文件的类型
*
* @param f
* @return
*/
private static String getMIMEType(File f) {
String end = f.getName().substring(f.getName().lastIndexOf(".") + 1,
f.getName().length()).toLowerCase();
String type;
if (end.equals("mp3") || end.equals("aac") || end.equals("amr") || end.equals("mpeg") || end.equals("mp4")) {
type = "audio";
} else if (end.equals("jpg") || end.equals("gif") || end.equals("png") || end.equals("jpeg")) {
type = "image";
} else if (end.equals("doc") || end.equals("docx") || end.equals("pdf")
|| end.equals("txt")) {
type = "application/msword";
return type;
} else {
type = "*";
}
type += "/*";
return type;
}

/**
* 如果是wps文件,则用wps打开,并对其设置一下参数
*
* @param file
* @return
*/
public static Intent openWpsFile(File file) {
Intent intent = new Intent();
Bundle bundle = new Bundle();
//打开模式(正常:Normal|正常:Normal|只读:ReadOnly|正常:Normal|)
bundle.putString("OpenMode", "Normal");
//保存文件的广播
bundle.putBoolean("SendSaveBroad", false);
//关闭文件的广播
bundle.putBoolean("SendCloseBroad", false);
//清除临时文件
bundle.putBoolean("IsClearBuffer", true);
//清除使用记录,应该不起作用(正确:ClearTrace)
bundle.putBoolean("IsClearTrace", false);
//删除打开文件
bundle.putBoolean("IsClearFile", false);
//自动跳转,包括页数和xy坐标
bundle.putBoolean("AutoJump", false);
//第三方包名
// bundle.putString("ThirdPackage", x.app().getPackageName());
bundle.putString("ThirdPackage", MyApplication.getContext().getPackageName());
//保存路径
bundle.putString("SavePath", file.getPath());
//缓存文件是否可见
bundle.putBoolean("CacheFileInvisible", false);
//修订模式打开文档
bundle.putBoolean("EnterReviseMode", false);
//视图比例
bundle.putFloat("ViewScale", 1.0f);
//如果打开的文档是上次关闭的
//bundle.putFloat(Define.VIEW_PROGRESS, ViewProgress); //阅读进度
//bundle.putInt(Define.VIEW_SCROLL_X, ViewScrollX); //x
//bundle.putInt(Define.VIEW_SCROLL_Y, ViewScrollY); //y
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
if (checkPackage(PACKAGENAME_ENT)) {
intent.setClassName(PACKAGENAME_ENT, CLASSNAME);
} else if (checkPackage(PACKAGENAME)) {
intent.setClassName(PACKAGENAME, CLASSNAME);
} else if (checkPackage(PACKAGENAME_ENG)) {
intent.setClassName(PACKAGENAME_ENG, CLASSNAME);
} else if (checkPackage(PACKAGENAME_KING_ENT)) {
intent.setClassName(PACKAGENAME_KING_ENT, CLASSNAME);
} else if (checkPackage(PACKAGENAME_KING_PRO)) {
intent.setClassName(PACKAGENAME_KING_PRO, CLASSNAME);
} else if (checkPackage(PACKAGENAME_KING_PRO_HW)) {
intent.setClassName(PACKAGENAME_KING_PRO_HW, CLASSNAME);
}
Uri uri = Uri.fromFile(file);
intent.setData(uri);
intent.putExtras(bundle);
return intent;
}

/**
* 检查是否安装WPS
*/
public static boolean checkInstallWps() {
if (checkPackage(PACKAGENAME_ENT) || checkPackage(PACKAGENAME) ||
checkPackage(PACKAGENAME_ENG) || checkPackage(PACKAGENAME_KING_ENT) ||
checkPackage(PACKAGENAME_KING_PRO) || checkPackage(PACKAGENAME_KING_PRO_HW)) {
return true;
}
return false;
}

/**
* 检测该包名所对应的应用是否存在
*
* @param packageName
* @return
*/

public static boolean checkPackage(String packageName) {
if (packageName == null || "".equals(packageName))
return false;
// try {
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// x.app().getPackageManager().getApplicationInfo(packageName, PackageManager.MATCH_UNINSTALLED_PACKAGES);
// } else {
// x.app().getPackageManager().getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
// }
// return true;
// } catch (PackageManager.NameNotFoundException e) {
// return false;
// }
return PackageUtils.isInstall(MyApplication.getContext(), packageName);
}
}

 


上一篇:输出流复制文件
下一篇:没有了
网友评论