WPS工具类
public class WpsUtil {
public WpsInterface wpsInterface;
private WpsCloseListener wpsCloseListener = null;
private Boolean canWrite;
private Activity mActivity;
private String fileName;
private String fileUrl;
public WpsUtil(WpsInterface wpsInterface, String fileName, String fileUrl, Boolean canWrite, Activity activity) {
this.wpsInterface = wpsInterface;
this.canWrite = canWrite;
this.mActivity = activity;
this.fileName = fileName;
this.fileUrl = fileUrl;
}
public void openDocument() {
if (FileOpen.isAvilible(mActivity, "cn.wps.moffice_eng")) {
openDocument(null);
} else {
// 从市场上下载
Uri uri = Uri.parse("market://details?id=cn.wps.moffice_eng");
// 直接从指定网址下载
Intent it = new Intent(Intent.ACTION_VIEW, uri);
mActivity.startActivity(it);
}
}
public void openDocument(File file) {
try {
wpsCloseListener = new WpsCloseListener();
IntentFilter filter = new IntentFilter();
filter.addAction("com.kingsoft.writer.back.key.down");//按下返回键
filter.addAction("com.kingsoft.writer.home.key.down");//按下home键
filter.addAction("cn.wps.moffice.file.save");//保存
filter.addAction("cn.wps.moffice.file.close");//关闭
mActivity.registerReceiver(wpsCloseListener, filter);//注册广播
if (file != null) {
openDocWithSimple(file);
} else {
openDocFile();
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 打开本地文件
public void openDocWithSimple(File file) {
try {
Intent intent = mActivity.getPackageManager().getLaunchIntentForPackage("cn.wps.moffice_eng");
Bundle bundle = new Bundle();
//打开模式
bundle.putString(Define.OPEN_MODE, Define.NORMAL);
bundle.putBoolean(Define.ENTER_REVISE_MODE, true);//以修订模式打开
// bundle.putString(Define.OPEN_MODE, Define.READ_ONLY);
bundle.putBoolean(Define.SEND_SAVE_BROAD, true);
bundle.putBoolean(Define.SEND_CLOSE_BROAD, true);
bundle.putBoolean(Define.HOME_KEY_DOWN, true);
bundle.putBoolean(Define.BACK_KEY_DOWN, true);
bundle.putBoolean(Define.ENTER_REVISE_MODE, true);
bundle.putBoolean(Define.IS_SHOW_VIEW, false);
bundle.putBoolean(Define.AUTO_JUMP, true);
//设置广播
bundle.putString(Define.THIRD_PACKAGE, mActivity.getPackageName());
intent.setAction(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri contentUri = FileProvider.getUriForFile(mActivity,
mActivity.getPackageName() + ".fileProvider", file);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(contentUri, "*/*");
} else {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(file), "*/*");
}
intent.putExtras(bundle);
mActivity.startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
// 打开文档
boolean openDocFile() {
try {
Intent intent = mActivity.getPackageManager().getLaunchIntentForPackage("cn.wps.moffice_eng");
Bundle bundle = new Bundle();
if (canWrite) {
bundle.putString(Define.OPEN_MODE, Define.NORMAL);
bundle.putBoolean(Define.ENTER_REVISE_MODE, true);//以修订模式打开
} else {
bundle.putString(Define.OPEN_MODE, Define.READ_ONLY);
}
//打开模式
bundle.putBoolean(Define.SEND_SAVE_BROAD, true);
bundle.putBoolean(Define.SEND_CLOSE_BROAD, true);
bundle.putBoolean(Define.HOME_KEY_DOWN, true);
bundle.putBoolean(Define.BACK_KEY_DOWN, true);
bundle.putBoolean(Define.ENTER_REVISE_MODE, true);
bundle.putBoolean(Define.IS_SHOW_VIEW, false);
bundle.putBoolean(Define.AUTO_JUMP, true);
//设置广播
bundle.putString(Define.THIRD_PACKAGE, mActivity.getPackageName());
//第三方应用的包名,用于对改应用合法性的验证
// bundle.putBoolean(Define.CLEAR_FILE, true);
//关闭后删除打开文件
intent.setAction(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse(fileUrl));
intent.putExtras(bundle);
mActivity.startActivity(intent);
return true;
} catch (ActivityNotFoundException e) {
e.printStackTrace();
return false;
}
}
public interface WpsInterface {
void doRequest(String filePath);
void doFinish();
}
// 广播接收器
private class WpsCloseListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
if (intent.getAction().equals("cn.wps.moffice.file.save")) {
String fileSavePath = intent.getExtras().getString(Define.SAVE_PATH);
if (canWrite) {
wpsInterface.doRequest(fileSavePath);
}
} else if (intent.getAction().equals("cn.wps.moffice.file.close") ||
intent.getAction().equals("com.kingsoft.writer.back.key.down")) {
wpsInterface.doFinish();
mActivity.unregisterReceiver(wpsCloseListener);//注销广播
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void appBack() {
//获取ActivityManager
ActivityManager mAm = (ActivityManager) mActivity.getSystemService(Context.ACTIVITY_SERVICE);
//获得当前运行的task
List taskList = mAm.getRunningTasks(100);
//找到当前应用的task,并启动task的栈顶activity,达到程序切换到前台
for (ActivityManager.RunningTaskInfo rti : taskList) {
if (rti.topActivity.getPackageName().equals(mActivity.getPackageName())) {
Intent LaunchIntent = new Intent(Intent.ACTION_MAIN);
ComponentName cn = new ComponentName(mActivity.getPackageName(), rti.topActivity.getClassName());
LaunchIntent.setComponent(cn);
mActivity.startActivity(LaunchIntent);
break;
}
}
}
public static boolean isAvilible(Context context, String packageName) {
String tag = "isAvilible";
final PackageManager packageManager = context.getPackageManager();
// 获取所有已安装程序的包信息
List pinfo = packageManager.getInstalledPackages(0);
for (int i = 0; i < pinfo.size(); i++) {
Log.i(tag, "i=" + i + ", " + pinfo.get(i).packageName);
if (pinfo.get(i).packageName.equalsIgnoreCase(packageName))
return true;
}
return false;
}
public class Define
{
public static final String PREFS_NAME = "MyPrefsFile"; //用于存取参数的文件名
public static final String KEY = "PackageName"; //第三方包名
public static final String READ_ONLY = "ReadOnly"; //只读模式
public static final String NORMAL = "Normal"; //正常模式
public static final String READ_MODE = "ReadMode"; //打开文件直接进入阅读器模式
public static final String EDIT_MODE = "EditMode"; //打开文件直接进入编辑模式
public static final String SAVE_ONLY = "SaveOnly"; //仅仅用来另存文件
public static final String VIEW_SCALE = "ViewScale"; //视图比例
public static final String VIEW_PROGRESS = "ViewProgress"; //查看进度百分比
public static final String VIEW_SCROLL_X = "ViewScrollX"; //显示的x坐标
public static final String VIEW_SCROLL_Y = "ViewScrollY"; //显示的y坐标
public static final String CLOSE_FILE = "CloseFile"; //关闭的文件
public static final String OPEN_FILE = "OpenFile"; //打开的文件
public static final String THIRD_PACKAGE = "ThirdPackage"; //第三方的包名
public static final String SAVE_PATH = "SavePath"; //文件保存的路径
public static final String CLEAR_BUFFER = "ClearBuffer"; //清除缓冲区,默认为true
public static final String CLEAR_TRACE = "ClearTrace"; //清除使用痕迹,默认为false
public static final String CLEAR_FILE = "ClearFile"; //删除文件自身,默认为false
public static final String CHECK_PACKAGE_NAME = "CheckPackageName"; //企业版华为不固定的应用包名
public static final String IS_SCREEN_SHOTFORBID = "isScreenshotForbid"; // 禁止截屏
// 以下是自己重新定义的
public static final String USER_NAME = "UserName";
public static final String SEND_CLOSE_BROAD = "SendCloseBroad"; //关闭文件时是否发送广播,默认不发送
public static final String SEND_SAVE_BROAD = "SendSaveBroad"; //关闭保存时是否发送广播,默认不发送
public static final String IS_VIEW_SCALE = "IsViewScale"; //view scale
public static final String OPEN_MODE = "OpenMode"; //阅读器模式
public static final String AUTO_JUMP = "AutoJump"; //第三方打开文件时是否自动跳转
public static final String IS_CLEAR_BUFFER = "IsClearBuffer"; //清除缓冲区,默认为true
public static final String IS_CLEAR_TRACE = "IsClearTrace"; //清除使用痕迹,默认为false
public static final String IS_CLEAR_FILE = "IsClearFile"; //删除文件自身,默认为false
public static final String HOME_KEY_DOWN = "HomeKeyDown"; //Home 按钮
public static final String BACK_KEY_DOWN = "BackKeyDown"; //Back 按钮
public static final String CACHE_FILE_INVISIBLE = "CacheFileInvisible"; //缓存文件是否可见,默认可见
public static final String ENTER_REVISE_MODE = "EnterReviseMode"; //以修订模式打开文档
public static final String ENCRYPT_FILE = "EncrptFile"; //加密方式操作文档
public static final String MENU_XML = "MenuXML"; //xml菜单
public static final String REVISION_NOMARKUP = "RevisionNoMarkup";//修订模式的无标记
public static final String DISPLAY_OPEN_FILE_NAME = "DisplayOpenFileName"; // 第三方设置文档显示名称
public static final String SHOW_REVIEWING_PANE_RIGHT_DEFAULT = "ShowReviewingPaneRightDefault"; //打开文档默认侧边栏不显示/显示 默认false
public static final String WATERMASK_TEXT = "WaterMaskText";//水印文字
public static final String WATERMASK_COLOR = "WaterMaskColor";//水印文字颜色
public static final String PACKAGENAME_KING_PRO = "com.kingsoft.moffice_pro";
public static final String CLASSNAME = "cn.wps.moffice.documentmanager.PreStartActivity2"; //wps类名,标准版本
// public static final String CLASSNAME = "cn.wps.moffice.emm.EmmOpenFileActivity"; //wps类名,EMM版本
public static final String OFFICE_SERVICE_ACTION = "cn.wps.moffice.service.OfficeService";
public static final String PRO_OFFICE_SERVICE_ACTION = "cn.wps.moffice.service.ProOfficeService";
public static final String OFFICE_ACTIVITY_NAME = "cn.wps.moffice.service.MOfficeWakeActivity";
public static final String OFFICE_READY_ACTION = "cn.wps.moffice.service.startup";
public static final String WPS_OPEN_MODE = "WPSOPENMODE";
public static final String WPS_OPEN_AIDL = "AIDL";
public static final String WPS_OPEN_THIRD = "THIRD";
public static final String FAIR_COPY = "FairCopy"; //清稿
public static final String FAIR_COPY_PW = "FairCopyPw"; //清稿密码
public static final String IS_SHOW_VIEW = "IsShowView"; //是否显示wps界面
//自动播放控制
public static final String AUTO_PLAY = "AutoPlay"; // PPT直接进入自动播放
public static final String AUTO_PLAY_INTERNAL = "AutoPlayInternal"; // PPT自动播放间隔
//编辑
public static final int INVALID_EDITPARAM = -1;
public static final String AT_SAVE = "AT_SAVE"; //保存
public static final String AT_SAVEAS = "AT_SAVEAS"; //另存为
public static final String AT_COPY = "AT_COPY"; //复制
public static final String AT_CUT = "AT_CUT"; //剪切
public static final String AT_PASTE = "AT_PASTE"; //粘贴
// public static final String AT_EDIT_TEXT = "AT_EDIT_TEXT"; //插入文字
// public static final String AT_EDIT_PICTURE = "AT_EDIT_PICTURE"; //插入图片
// public static final String AT_EDIT_SHAPE = "AT_EDIT_SHAPE"; //插入浮动图片
// public static final String AT_EDIT_CHART = "AT_EDIT_CHART"; //编辑图表
public static final String AT_SHARE = "AT_SHARE"; //分享
public static final String AT_PRINT = "AT_PRINT"; //输出
public static final String AT_SPELLCHECK = "AT_SPELLCHECK"; //拼写检查
public static final String AT_QUICK_CLOSE_REVISEMODE = "AT_QUICK_CLOSE_REVISEMODE"; //快速关闭修订
public static final String AT_MULTIDOCCHANGE = "AT_MULTIDOCCHANGE"; //多文档切换
public static final String AT_EDIT_REVISION = "AT_EDIT_REVISION";
public static final String AT_CURSOR_MODEL = "AT_CURSOR_MODEL";
public static final String AT_PATH = "at_path"; //编辑路径
public static final String AT_CHANGE_COMMENT_USER = "AT_CHANGE_COMMENT_USER";
public static final String AT_SHARE_PLAY = "AT_SHARE_PLAY";
public static final String AT_GRID_BACKBOARD = "AT_GRID_BACKBOARD";
public static final String SERIAL_NUMBER_OTHER = "SerialNumberOther"; // android 激活外部传入序列号
public static final String SERIAL_NUMBER_OTHERPC = "SerialNumberOtherPc"; // PC 激活外部传入序列号
public static final String SCREEN_ORIENTATION = "ScreenOrientation";
public static final String ZOOM_OTHER = "zoom";
public static final String JSON_DATA =
"[" +
"{ \"name\" : \"cn.wps.moffice.client.OfficeServiceClient\"," +
" \"type\" : \"Package-ID\",\"id\" : \"cn.wps.moffice.client\", " +
"\"Security-Level\" : \"Full-access\", \"Authorization\" : \"abxxdsewrwsds3232ss\" }," +
"]";
}
引用
//线上地址url
wpsUtil = new WpsUtil(this, "",url, false, SiteSurveyDetailActivity.this);
wpsUtil.openDocument();
//本地文件url的getAbsolutePath()
wpsUtil = new WpsUtil(this,url, "", false, this);
wpsUtil.openDocument(new File(fileInfoList.get(position).getAbsolutePath()));