【開源項目】智能電視及電視盒子的控制應用TVRemoteIME的接口說明

1、APP項目介紹:

APP名稱:TVRemoteIMEhtml

功能說明:安卓智能電視或者安卓盒子的控制應用,可跨屏遠程輸入、遠程遙控(代替遙控器)盒子、盒子應用及文件管理、HTTP/RTMP/MMS網絡視頻直播、ED2K/種子文件的視頻文件邊下邊播java

項目地址:https://github.com/kingthy/TVRemoteIMEandroid

APK包下載:https://github.com/kingthy/TVRemoteIME/raw/master/released/IMEService-release.apkgit

 

2、APP控制端界面截圖

控制端不須要安裝任何APK應用,直接在同局域網的手機、電腦或者PAD的瀏覽器訪問操做github

輸入遙控:瀏覽器

應用管理:網絡

 示例截圖

 

文件管理:app

 

示例截圖

 

視頻直播:ide

示例截圖

 

 

3、APP核心功能介紹

一、IMEService:APP的主核心入口,繼承於InputMethodService,用於實現輸入服務,提供簡易的QWERT鍵盤UI,核心的遙控功能也是經過此服務來實現的。post

二、RemoteServer: APP的Web API服務,經過NanoHTTPD實現Web服務,控制端的全部操做都是直接調用此服務下的WebAPI接口實現。

三、ijkplayer項目: 視頻播放器項目,經過封裝ijkplayer播放器實現,封閉實現了視頻源的解析及下載,支持HTTP/RTMP/MMS網絡視頻直播、ED2K/種子文件的視頻文件邊下邊播

四、thunder項目: 第三方的下載服務,實現視頻源的下載服務

 五、index.html與ime_core.js: 實現控制端的功能頁面及腳本代碼,全部控制端的操做都是經過此頁面和腳本實現。

 

4、WebAPI接口介紹

一、「/text」      :文本輸入接口,用於實現跨屏輸入

代碼文件:InputRequestProcesser.java

參數:

text  = 要輸入的文本,能夠爲任意數量的字符

調用示例:

POST /text

text=要輸入的文字

代碼實現:

Web服務接收到接口請求後經過DataReceiver的onTextReceived事件方法通知輸入法服務IMEService,輸入法服務經過commitText方法將遠程輸入的文本提交輸入到相應的輸入框。

    private boolean commitText(String text){
        InputConnection ic = getCurrentInputConnection(); boolean flag = false; if (ic != null){ Log.d(TAG, "commitText:" + text); if(text.length() > 1 && ic.beginBatchEdit()){ flag = ic.commitText(text, 1); ic.endBatchEdit(); }else{ flag = ic.commitText(text, 1); } } return flag; }

 

二、「/key",「/keydown「,」/keyup「: 三個接口分別實現按鍵不一樣的輸入狀態:」按擊「,」按下「,」彈起「。 可用於實現按鍵操做,如遠程遙控功能。

代碼文件:InputRequestProcesser.java

參數:

code = 按鍵碼,對應於安卓KeyEvent裏定義的按鍵碼,特殊定義的按鍵碼」cls「表示「清空文本「

調用示例:

POST /key

code=cls

 

POST /keydown

code=67

代碼實現:

Web服務接收到接口請求後經過DataReceiver的onKeyEventReceived事件方法通知輸入法服務IMEService,輸入法服務再根據按鍵碼及按鍵動做進行處理。

                @Override
                public void onKeyEventReceived(String keyCode, int keyAction) { if(keyCode != null) { if("cls".equalsIgnoreCase(keyCode)){ InputConnection ic = getCurrentInputConnection(); if(ic != null) { ic.performContextMenuAction(android.R.id.selectAll); ic.commitText("", 1); } }else { final int kc = KeyEvent.keyCodeFromString(keyCode); if(kc != KeyEvent.KEYCODE_UNKNOWN){ if(mInputView != null && KeyEventUtils.isKeyboardFocusEvent(kc) && mInputView.isShown()){ if(keyAction == KEY_ACTION_PRESSED || keyAction == KEY_ACTION_DOWN) { handler.post(new Runnable() { @Override public void run() { if (!handleKeyboardFocusEvent(kc)) { sendKeyCode(kc); } } }); } } else{ long eventTime = SystemClock.uptimeMillis(); InputConnection ic = getCurrentInputConnection(); switch (keyAction) { case KEY_ACTION_PRESSED: sendKeyCode(kc); break; case KEY_ACTION_DOWN: if(ic != null) { ic.sendKeyEvent(new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_DOWN, kc, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE)); } break; case KEY_ACTION_UP: if(ic != null) { ic.sendKeyEvent(new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_UP, kc, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE)); } break; } } } } } }

 

三、「/apps」 : 獲取盒子上安裝的APP數據列表,實現盒子APP的管理

 代碼文件:AppRequestProcesser.java

參數:

system= 是否包含系統應用,true或false

調用示例:

POST /apps

system=false

代碼實現:

經過PackageManager查詢盒子上安裝的APP列表,在處理列表數據的同時過濾掉系統底層的APP(非第三方)。

    public static List<AppInfo> queryAppInfo(Context context, boolean containSysApp){
        PackageManager pm = context.getPackageManager(); List<ApplicationInfo> listAppcations = pm .getInstalledApplications(PackageManager.MATCH_UNINSTALLED_PACKAGES); List<AppInfo> appInfos = new ArrayList<AppInfo>(); for (ApplicationInfo app : listAppcations) { if(containSysApp || (app.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { boolean isSysApp = (app.flags & ApplicationInfo.FLAG_SYSTEM) != 0; //過濾掉系統底層的app if(isSysApp && (app.packageName.startsWith("com.android.") || app.packageName.equals("android")))continue; AppInfo appInfo = new AppInfo(); appInfo.setLable((String) app.loadLabel(pm)); appInfo.setPackageName(app.packageName); appInfo.setApkPath(app.sourceDir); appInfo.setSysApp(isSysApp); appInfos.add(appInfo); } } Collections.sort(appInfos, new Comparator<AppInfo>() { @Override public int compare(AppInfo o1, AppInfo o2) { int i1 = (o1.isSysApp ? 2 : 1); int i2 = (o2.isSysApp ? 2 : 1); if(i1 == i2){ return o1.getLable().compareTo(o2.getLable()); }else{ return Integer.compare(i1, i2); } } }); return appInfos; }

 

四、「/uninstall」 : 卸載應用接口

 代碼文件:AppRequestProcesser.java

參數:

packageName=卸載的應用包名

調用示例:

POST /uninstall

packageName=com.test.app

代碼實現:

直接調用系統的卸載服務。

    public static void uninstallPackage(final String packageName, final Context context){
        if(getApplicationInfo(packageName, context) == null)return;; try { Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(Intent.ACTION_DELETE); intent.setData(Uri.parse("package:" + packageName)); context.startActivity(intent); Log.i(IMEService.TAG, String.format("已刪除應用包[%s]", packageName)); }catch (Exception ex){ Log.e(IMEService.TAG, String.format("刪除應用包[%s]出錯", packageName), ex); } }

 

五、「/run」 ,「/runSystem」: 運行應用及或調用系統應用服務,如彈出系統設置界面。

 代碼文件:AppRequestProcesser.java

參數:

packageName=要運行的應用包名

調用示例:

POST /run

packageName=com.test.app

 

POST /runSystem

packageName=android.settings.SETTINGS

代碼實現:

運行第三方應用則是經過PackageManager獲取包裏的啓動頁後再運行,調用系統應用服務則是經過Intent直接啓動。

    public static void runPackage(final String packageName, final Context context){
        if(getApplicationInfo(packageName, context) == null)return;; try { PackageManager pm = context.getPackageManager(); Intent intent = pm.getLaunchIntentForPackage(packageName); if(intent != null){ context.startActivity(intent); } Log.i(IMEService.TAG, String.format("已運行應用包[%s]", packageName)); }catch (Exception ex){ Log.e(IMEService.TAG, String.format("運行應用包[%s]出錯", packageName), ex); } } public static void runSystemPackage(final String packageName, final Context context){ try { Intent intent = new Intent(packageName); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); Log.i(IMEService.TAG, String.format("已運行系統應用包[%s]", packageName)); }catch (Exception ex){ Log.e(IMEService.TAG, String.format("運行系統應用包[%s]出錯", packageName), ex); } }

 

六、「/upload」 : 文件傳送接口。「輸入遙控」界面下的文件傳送接口,若是傳送的爲APK包則可自動安裝,若是爲視頻文件或者種子文件則可自動播放。

 代碼文件:UploadRequestProcesser.java

參數:

file=上傳的文件

autoPlay = 是否自動安裝APK包或者自動播放視頻及種子文件,true或者false

調用示例:

POST /upload

-------

代碼實現:

直接經過NanoHTTPD的文件上傳功能實現,爲避免跨"分區「移動文件,重寫了NanoHTTPD的TempFileManager實現,以便文件上傳時直接上傳到IMEService數據目錄下的files目錄。

代碼請參考RemoteServerFileManager.java文件

 

七、「/play」 : 視頻播放服務接口

 代碼文件:PlayRequestProcesser.java

參數:

playUrl=要播放的視頻源地址

調用示例:

POST /play

playUrl=http://www.test.com/test.avi

代碼實現:

直接將視頻源地址傳入ijkplayer項目的播放器進行播放

XLVideoPlayActivity.intentTo(context, url, url);

 

八、「/file/dir/」 : 文件管理接口服務,實現盒子的文件及目錄查看功能。

 代碼文件:FileRequestProcesser.java

參數:

調用示例:

GET /file/dir/{查看的文件或者目錄路徑}

代碼實現:

獲取目錄下的子目錄及文件列表,返回JSON數據

   private NanoHTTPD.Response responseDirData(String dirName) {
        File path = new File(Environment.getExternalStorageDirectory(), dirName); String root = Environment.getExternalStorageDirectory().getPath(); JSONArray dirs = new JSONArray(); JSONArray files = new JSONArray(); try { File[] subfiles = path.listFiles(); Arrays.sort(subfiles, new Comparator<File>() { @Override public int compare(File f1, File f2) { return f1.getName().toLowerCase().compareTo(f2.getName().toLowerCase()); } }); for (File file : subfiles) { if(file.isHidden()) continue; JSONObject item = new JSONObject(); item.put("name", file.getName()); item.put("path", file.getPath().substring(root.length())); if (file.isDirectory()) { //item.put("total", 0);  dirs.put(item); }else { item.put("size", file.length()); files.put(item); } } JSONObject data = new JSONObject(); if(!TextUtils.isEmpty(dirName) && dirName != "/") data.put("parent", path.getParent().substring(root.length())); data.put("dirs", dirs); data.put("files", files); return RemoteServer.createJSONResponse(NanoHTTPD.Response.Status.OK, data.toString()); }catch (JSONException ex){ return RemoteServer.createPlainTextResponse(NanoHTTPD.Response.Status.INTERNAL_ERROR, "SERVER INTERNAL ERROR: JSONException: " + ex.getMessage()); } }

 

九、「/file/download/」 : 文件下載服務,實現盒子的文件下載功能。

 代碼文件:FileRequestProcesser.java

參數:

調用示例:

GET /file/download/{下載的文件路徑}

代碼實現:

    private NanoHTTPD.Response downloadFileData(String fileName){
        File file = new File(Environment.getExternalStorageDirectory(), fileName); if(!file.exists()){ return RemoteServer.createPlainTextResponse(NanoHTTPD.Response.Status.NOT_FOUND, "Error 404, file not found."); } try{ InputStream inputStream = new FileInputStream(file); return RemoteServer.newFixedLengthResponse(NanoHTTPD.Response.Status.OK, NanoHTTPD.getMimeTypeForFile(file.getName()) + "; charset=utf-8", inputStream, (long)inputStream.available()); } catch (Exception e) { return RemoteServer.createPlainTextResponse(NanoHTTPD.Response.Status.NOT_FOUND, "Error 404, file not found."); } }

 

十、「/file/cut」 ,「/file/copy」: 文件與目錄的剪切、複製服務。

 代碼文件:FileRequestProcesser.java

參數:

paths = 要操做的目錄或者文件列表,多個之間用「|」分隔開

targetPath =  剪切或者移動的文件的新目標目錄

調用示例:

POST /file/cut

paths=/Download/a.txt&targetPath=/Andorid/data/
POST /file/copy

paths=/Download/a.txt&targetPath=/Andorid/data/

代碼實現:

   private void batchCopyFile(String targetPath, String paths){
        File targetPathFile = new File(Environment.getExternalStorageDirectory(), targetPath); if(!targetPathFile.exists()) return; String[] pathData = paths.split("\\|"); for(String p : pathData){ if(!TextUtils.isEmpty(p)) { File source = new File(Environment.getExternalStorageDirectory(), p); RemoteServerFileManager.copyFile(source, targetPathFile); } } } private void batchCutFile(String targetPath, String paths){ File targetPathFile = new File(Environment.getExternalStorageDirectory(), targetPath); if(!targetPathFile.exists()) return; String[] pathData = paths.split("\\|"); for(String p : pathData){ if(!TextUtils.isEmpty(p)) { File source = new File(Environment.getExternalStorageDirectory(), p); RemoteServerFileManager.cutFile(source, targetPathFile); } } }

 

十一、「/file/delete」 : 文件或者目錄刪除服務,若是是目錄刪除則會一併刪除目錄下的全部子目錄和文件。

 代碼文件:FileRequestProcesser.java

參數:

paths = 要刪除的文件或者目錄列表

調用示例:

POST /file/delete

paths=/Download/a.txt|/Download/test

代碼實現:

    private void batchDeleteFile(String paths){
        String[] pathData = paths.split("\\|"); for(String p : pathData){ if(!TextUtils.isEmpty(p)) { File path = new File(Environment.getExternalStorageDirectory(), p); RemoteServerFileManager.deleteFile(path); } } }

 

十二、「/file/upload」 : 文件上傳服務。此接口和/upload接口相似,可是此接口是將文件上傳到文件管理界面下的當前目錄,而且不會安裝APK包和播放視頻文件。

 代碼文件:FileRequestProcesser.java

參數:

file = 上傳的文件數據

path = 存儲上傳文件的目錄

調用示例:

POST /file/upload

--------

代碼實現:

文件上傳NanoHTTPD內部已實現好,所以只須要將上傳的文件移動到對應的目錄便可。

    private NanoHTTPD.Response uploadFile(Map<String, String> params, Map<String, String> files){
        String uploadFileName  = params.get("file"); String uploadPathName = params.get("path"); String localFilename = files.get("file"); boolean r = false; if(!TextUtils.isEmpty(uploadFileName)) { if (!TextUtils.isEmpty(localFilename)) { File saveFilename = new File(Environment.getExternalStorageDirectory(), uploadPathName); File localFile = new File(localFilename); saveFilename = new File(saveFilename,localFile.getName()); r = localFile.renameTo(saveFilename); } } return RemoteServer.createJSONResponse(NanoHTTPD.Response.Status.OK, "{\"success\":" + (r ? "true": "false") + "}"); }

 

 五:附註

項目地址:https://github.com/kingthy/TVRemoteIME

交流QQ羣:7149348, 加入QQ羣能夠分享直播源、反饋問題及建議。

相關文章
相關標籤/搜索