Cordova app 检查更新 ----创建项目、添加插件、修改插件(一)

使用Cordova 进行跨平台应用程序的开发

1.创建Cordova项目

$ cordova create hello com.example.hello HelloWorld

2.添加插件

2.1切换到Plugins目录

2.2 添加一下插件

cordova plugin add cordova-plugin-device

cordova plugin add cordova-plugin-file

cordova plugin add cordova-plugin-file-transfer

cordova plugin add https://github.com/pwlin/cordova-plugin-file-opener2

3.修改插件

3.1需要修改Android项目的插件:

3.1.1修改cordova-plugin-file-transfer 下边的 FileTransfer.java 文件 ,引入import android.os.Environment;

/**
     *  获取当前目录在sdcard中的路径
     *  @param rootFolder   根目录
     * @param aFolder   当前目录
     */
    public String getStorageDirectory(String rootFolder,String aFolder){
        String storagePath= Environment.getExternalStorageDirectory().getPath()+"/"+rootFolder+"/"+aFolder;
        return storagePath;
    }

    /**
     * 创建目录
     * @param  fileDirectory 目录名称
     */
    public File createDirectory(String fileDirectory){
        File sdcardFile=new File(fileDirectory);
        if(!sdcardFile.exists()){
            sdcardFile.mkdirs();
        }
        return sdcardFile;
    }

    /**
     * Downloads a file form a given URL and saves it to the specified directory.
     *
     * @param source        URL of the server to receive the file
     * @param target            Full path of the file on the file system
     */
    private void download(final String source, final String target, JSONArray args, CallbackContext callbackContext) throws JSONException {
        Log.d(LOG_TAG, "download " + source + " to " + target);

        String localPath=this.getStorageDirectory("GaussQGY", "download");
        this.createDirectory(localPath);

        final CordovaResourceApi resourceApi = webView.getResourceApi();

        final boolean trustEveryone = args.optBoolean(2);
        final String objectId = args.getString(3);
        final JSONObject headers = args.optJSONObject(4);

        final Uri sourceUri = resourceApi.remapUri(Uri.parse(source));
        final String targetPath = localPath +‘/‘+ target;
        Log.e(LOG_TAG, "文件存放路径: " + targetPath);
        // Accept a path or a URI for the source.
        Uri tmpTarget = Uri.parse(targetPath);
        final Uri targetUri = resourceApi.remapUri(
            tmpTarget.getScheme() != null ? tmpTarget : Uri.fromFile(new File(targetPath)));

        int uriType = CordovaResourceApi.getUriType(sourceUri);
        final boolean useHttps = uriType == CordovaResourceApi.URI_TYPE_HTTPS;
        final boolean isLocalTransfer = !useHttps && uriType != CordovaResourceApi.URI_TYPE_HTTP;
        if (uriType == CordovaResourceApi.URI_TYPE_UNKNOWN) {
            JSONObject error = createFileTransferError(INVALID_URL_ERR, source, targetPath, null, 0, null);
            Log.e(LOG_TAG, "Unsupported URI: " + sourceUri);
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, error));
            return;
        }

        /* This code exists for compatibility between 3.x and 4.x versions of Cordova.
         * Previously the CordovaWebView class had a method, getWhitelist, which would
         * return a Whitelist object. Since the fixed whitelist is removed in Cordova 4.x,
         * the correct call now is to shouldAllowRequest from the plugin manager.
         */
        Boolean shouldAllowRequest = null;
        if (isLocalTransfer) {
            shouldAllowRequest = true;
        }
        if (shouldAllowRequest == null) {
            try {
                Method gwl = webView.getClass().getMethod("getWhitelist");
                Whitelist whitelist = (Whitelist)gwl.invoke(webView);
                shouldAllowRequest = whitelist.isUrlWhiteListed(source);
            } catch (NoSuchMethodException e) {
            } catch (IllegalAccessException e) {
            } catch (InvocationTargetException e) {
            }
        }
        if (shouldAllowRequest == null) {
            try {
                Method gpm = webView.getClass().getMethod("getPluginManager");
                PluginManager pm = (PluginManager)gpm.invoke(webView);
                Method san = pm.getClass().getMethod("shouldAllowRequest", String.class);
                shouldAllowRequest = (Boolean)san.invoke(pm, source);
            } catch (NoSuchMethodException e) {
            } catch (IllegalAccessException e) {
            } catch (InvocationTargetException e) {
            }
        }

        if (!Boolean.TRUE.equals(shouldAllowRequest)) {
            Log.w(LOG_TAG, "Source URL is not in white list: ‘" + source + "‘");
            JSONObject error = createFileTransferError(CONNECTION_ERR, source, targetPath, null, 401, null);
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, error));
            return;
        }

        final RequestContext context = new RequestContext(source, targetPath, callbackContext);
        synchronized (activeRequests) {
            activeRequests.put(objectId, context);
        }

        cordova.getThreadPool().execute(new Runnable() {
            public void run() {
                if (context.aborted) {
                    return;
                }
                HttpURLConnection connection = null;
                HostnameVerifier oldHostnameVerifier = null;
                SSLSocketFactory oldSocketFactory = null;
                File file = null;
                PluginResult result = null;
                TrackingInputStream inputStream = null;
                boolean cached = false;

                OutputStream outputStream = null;
                try {
                    OpenForReadResult readResult = null;

                    file = resourceApi.mapUriToFile(targetUri);
                    context.targetFile = file;

                    Log.d(LOG_TAG, "Download file:" + sourceUri);

                    FileProgressResult progress = new FileProgressResult();

                    if (isLocalTransfer) {
                        readResult = resourceApi.openForRead(sourceUri);
                        if (readResult.length != -1) {
                            progress.setLengthComputable(true);
                            progress.setTotal(readResult.length);
                        }
                        inputStream = new SimpleTrackingInputStream(readResult.inputStream);
                    } else {
                        // connect to server
                        // Open a HTTP connection to the URL based on protocol
                        connection = resourceApi.createHttpConnection(sourceUri);
                        if (useHttps && trustEveryone) {
                            // Setup the HTTPS connection class to trust everyone
                            HttpsURLConnection https = (HttpsURLConnection)connection;
                            oldSocketFactory = trustAllHosts(https);
                            // Save the current hostnameVerifier
                            oldHostnameVerifier = https.getHostnameVerifier();
                            // Setup the connection not to verify hostnames
                            https.setHostnameVerifier(DO_NOT_VERIFY);
                        }

                        connection.setRequestMethod("GET");

                        // TODO: Make OkHttp use this CookieManager by default.
                        String cookie = getCookies(sourceUri.toString());

                        if(cookie != null)
                        {
                            connection.setRequestProperty("cookie", cookie);
                        }

                        // This must be explicitly set for gzip progress tracking to work.
                        connection.setRequestProperty("Accept-Encoding", "gzip");

                        // Handle the other headers
                        if (headers != null) {
                            addHeadersToRequest(connection, headers);
                        }

                        connection.connect();
                        if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
                            cached = true;
                            connection.disconnect();
                            Log.d(LOG_TAG, "Resource not modified: " + source);
                            JSONObject error = createFileTransferError(NOT_MODIFIED_ERR, source, targetPath, connection, null);
                            result = new PluginResult(PluginResult.Status.ERROR, error);
                        } else {
                            if (connection.getContentEncoding() == null || connection.getContentEncoding().equalsIgnoreCase("gzip")) {
                                // Only trust content-length header if we understand
                                // the encoding -- identity or gzip
                                if (connection.getContentLength() != -1) {
                                    progress.setLengthComputable(true);
                                    progress.setTotal(connection.getContentLength());
                                }
                            }
                            inputStream = getInputStream(connection);
                        }
                    }

                    if (!cached) {
                        try {
                            synchronized (context) {
                                if (context.aborted) {
                                    return;
                                }
                                context.connection = connection;
                            }

                            // write bytes to file
                            byte[] buffer = new byte[MAX_BUFFER_SIZE];
                            int bytesRead = 0;
                            outputStream = resourceApi.openOutputStream(targetUri);
                            while ((bytesRead = inputStream.read(buffer)) > 0) {
                                outputStream.write(buffer, 0, bytesRead);
                                // Send a progress event.
                                progress.setLoaded(inputStream.getTotalRawBytesRead());
                                PluginResult progressResult = new PluginResult(PluginResult.Status.OK, progress.toJSONObject());
                                progressResult.setKeepCallback(true);
                                context.sendPluginResult(progressResult);
                            }
                        } finally {
                            synchronized (context) {
                                context.connection = null;
                            }
                            safeClose(inputStream);
                            safeClose(outputStream);
                        }

                        Log.d(LOG_TAG, "Saved file: " + targetPath);

                        // create FileEntry object
                        Class webViewClass = webView.getClass();
                        PluginManager pm = null;
                        try {
                            Method gpm = webViewClass.getMethod("getPluginManager");
                            pm = (PluginManager) gpm.invoke(webView);
                        } catch (NoSuchMethodException e) {
                        } catch (IllegalAccessException e) {
                        } catch (InvocationTargetException e) {
                        }
                        if (pm == null) {
                            try {
                                Field pmf = webViewClass.getField("pluginManager");
                                pm = (PluginManager)pmf.get(webView);
                            } catch (NoSuchFieldException e) {
                            } catch (IllegalAccessException e) {
                            }
                        }
                        file = resourceApi.mapUriToFile(targetUri);
                        context.targetFile = file;
                        FileUtils filePlugin = (FileUtils) pm.getPlugin("File");
                        if (filePlugin != null) {
                            JSONObject fileEntry = filePlugin.getEntryForFile(file);
                            if (fileEntry != null) {
                                result = new PluginResult(PluginResult.Status.OK, fileEntry);
                            } else {
                                JSONObject error = createFileTransferError(CONNECTION_ERR, source, targetPath, connection, null);
                                Log.e(LOG_TAG, "File plugin cannot represent download path");
                                result = new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
                            }
                        } else {
                            Log.e(LOG_TAG, "File plugin not found; cannot save downloaded file");
                            result = new PluginResult(PluginResult.Status.ERROR, "File plugin not found; cannot save downloaded file");
                        }
                    }
                } catch (FileNotFoundException e) {
                    JSONObject error = createFileTransferError(FILE_NOT_FOUND_ERR, source, targetPath, connection, e);
                    Log.e(LOG_TAG, error.toString(), e);
                    result = new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
                } catch (IOException e) {
                    JSONObject error = createFileTransferError(CONNECTION_ERR, source, targetPath, connection, e);
                    Log.e(LOG_TAG, error.toString(), e);
                    result = new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
                } catch (JSONException e) {
                    Log.e(LOG_TAG, e.getMessage(), e);
                    result = new PluginResult(PluginResult.Status.JSON_EXCEPTION);
                } catch (Throwable e) {
                    JSONObject error = createFileTransferError(CONNECTION_ERR, source, targetPath, connection, e);
                    Log.e(LOG_TAG, error.toString(), e);
                    result = new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
                } finally {
                    synchronized (activeRequests) {
                        activeRequests.remove(objectId);
                    }

                    if (connection != null) {
                        // Revert back to the proper verifier and socket factories
                        if (trustEveryone && useHttps) {
                            HttpsURLConnection https = (HttpsURLConnection) connection;
                            https.setHostnameVerifier(oldHostnameVerifier);
                            https.setSSLSocketFactory(oldSocketFactory);
                        }
                    }

                    if (result == null) {
                        result = new PluginResult(PluginResult.Status.ERROR, createFileTransferError(CONNECTION_ERR, source, targetPath, connection, null));
                    }
                    // Remove incomplete download.
                    if (!cached && result.getStatus() != PluginResult.Status.OK.ordinal() && file != null) {
                        file.delete();
                    }
                    context.sendPluginResult(result);
                }
            }
        });
    }

3.1.2 修改Android项目下的 cordova-plugin-file-opener2 找到FileOpener2.java ,然后引入 import android.os.Environment;

修改_open方法

private void _open(String fileArg, String contentType, CallbackContext callbackContext) throws JSONException {
        String fileName = "";
        String filePath = Environment.getExternalStorageDirectory().getPath() + fileArg;
        try {
            CordovaResourceApi resourceApi = webView.getResourceApi();
            Uri fileUri = resourceApi.remapUri(Uri.parse(filePath));
            fileName = this.stripFileProtocol(fileUri.toString());
        } catch (Exception e) {
            fileName = filePath;
        }
        File file = new File(fileName);
        if (file.exists()) {
            try {
                Uri path = Uri.fromFile(file);
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(path, contentType);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                /*
                 * @see
                 * http://stackoverflow.com/questions/14321376/open-an-activity-from-a-cordovaplugin
                 */
                cordova.getActivity().startActivity(intent);
                //cordova.getActivity().startActivity(Intent.createChooser(intent,"Open File in..."));
                callbackContext.success();
            } catch (android.content.ActivityNotFoundException e) {
                JSONObject errorObj = new JSONObject();
                errorObj.put("status", PluginResult.Status.ERROR.ordinal());
                errorObj.put("message", "Activity not found: " + e.getMessage());
                callbackContext.error(errorObj);
            }
        } else {
            JSONObject errorObj = new JSONObject();
            errorObj.put("status", PluginResult.Status.ERROR.ordinal());
            errorObj.put("message", "File not found");
            callbackContext.error(errorObj);
        }
    }

3.2 需要修改iOS项目中的Cordova 插件 下的 cordova-plugin-file-opener2

3.2.1 修改FileOpener2.h

定义一个openURL 方法

#import <Cordova/CDV.h>

@interface FileOpener2 : CDVPlugin <UIDocumentInteractionControllerDelegate> {
    NSString *localFile;
}

@property(nonatomic, strong) UIDocumentInteractionController *controller;

- (void) open: (CDVInvokedUrlCommand*)command;
- (void) openURL: (CDVInvokedUrlCommand*)command;

@end

3.2.2 修改FileOpener2.m

实现FileOpener2.h中定义的openURL 方法

  

#import "FileOpener2.h"
#import <Cordova/CDV.h>

#import <QuartzCore/QuartzCore.h>
#import <MobileCoreServices/MobileCoreServices.h>

@implementation FileOpener2

#pragma mask --- 加载其它应用
- (void)openURL:(CDVInvokedUrlCommand *)command
{
    NSString *path = command.arguments[0];
    NSURL *url= [NSURL URLWithString: path];
    [[UIApplication sharedApplication] openURL:url];
}

#pragma mask --- 打开本地文件
- (void) open: (CDVInvokedUrlCommand*)command {

    NSString *path = command.arguments[0];
    NSString *uti = command.arguments[1];
    if (!uti || (NSNull*)uti == [NSNull null]) {
        NSArray *dotParts = [path componentsSeparatedByString:@"."];
        NSString *fileExt = [dotParts lastObject];

        uti = (__bridge NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)fileExt, NULL);
    }

    CDVViewController* cont = (CDVViewController*)[ super viewController ];

    dispatch_async(dispatch_get_main_queue(), ^{
        // TODO: test if this is a URI or a path
        NSURL *fileURL = [NSURL URLWithString:path];

        localFile = fileURL.path;

        NSLog(@"looking for file at %@", fileURL);
        NSFileManager *fm = [NSFileManager defaultManager];
        if(![fm fileExistsAtPath:localFile]) {
            NSDictionary *jsonObj = @{@"status" : @"9",
                                      @"message" : @"File does not exist"};
            CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
                                                          messageAsDictionary:jsonObj];
            [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
            return;
        }

        self.controller = [UIDocumentInteractionController  interactionControllerWithURL:fileURL];
        self.controller.delegate = self;
        self.controller.UTI = uti;

        CGRect rect = CGRectMake(0, 0, 1000.0f, 150.0f);
        CDVPluginResult* pluginResult = nil;
        BOOL wasOpened = [self.controller presentOptionsMenuFromRect:rect inView:cont.view animated:NO];

        if(wasOpened) {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: @""];
        } else {
            NSDictionary *jsonObj = @{@"status" : @"9",
                                      @"message" : @"Could not handle UTI"};
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
                                         messageAsDictionary:jsonObj];
        }
        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    });
}

@end

     

时间: 2024-09-29 02:24:05

Cordova app 检查更新 ----创建项目、添加插件、修改插件(一)的相关文章

Android Cordova 插件开发之创建项目

使用命令行创建项目 cordova 创建项目的命令是 cordova create <dir> <packageName> <projectName> 第一个参数 < dir >:项目目录文件夹的名称 第二个参数< packageName >:项目的包名 第三个参数< projectName >:项目名称  那么,我们创建一个HelloWorld项目,通过命令行cd到项目要创建的根目录下,然后执行命令 cordova create h

安装配置PhoneGap开发环境(二)——使用Cordova代替PhoneGap创建项目

1 Cordova是谁 PhoneGap的官方文档说的很清楚.Cordova是PhoneGap的引擎,这两者的关系类似于WebKit与Chrome浏览器的关系.所以一些核心的基础操作对于Cordova与PhoneGap是相通的.有时候使用PhoneGap创建工程的会出现一些莫名的问题,推荐使用Cordova创建. 2 下载PhoneGap与Cordova 略.见<安装配置PhoneGap开发环境(一)>. 3 使用Cordova创建PhoneGap项目 查看当前平台的Cordova版本: co

cordova APP 检查更新

//升级程序 .factory('UpdateService', function ($rootScope, $cordovaAppVersion, $cordovaFileTransfer, $timeout, $ionicLoading, $cordovaFileOpener2, $http, $ionicPopup, xc) { //检查更新 var checkUpdate = function () { $cordovaAppVersion.getVersionNumber().then

Ionic3,安装,创建项目

Ionic3 简介:是一款html5的轻量级手机开发框架,基于angularjs.scss语法,ionic是一个轻量的手机UI库.并直接放弃了IOS 6和Android 4.1一下的版本支持. 安装: 第一步:安装node.js,https://nodejs.org/en/download/. node.js:是一个javascript的运行环境,实质是对Chrome V8引擎进行了封装,提供了相关的API,使得在非浏览器的环境下,V8依然还能够正常运行. 第二步:安装cordova,cordo

学习制作iOS程序第二天:创建子目录、更改项目名称、修改启动画面、修改类前缀、新建启动控制器、修改APP图标

四.根据实际情况创建相应的目录 删除系统默认的部分文件ViewController.h,ViewController.m,Main.storyboard.LaunchScreen.xib 目录根据情况创建,每个人都会不一样的.我的如下. 五:更改项目名称 1.进入项目的Targets属性,找到Build Settings,搜索Product Name,修改属性为真实的软件名称. 2.打开Supporting Files目录下的Info.plist,修改Bundle name为真实的软件名称. 3

Phonegap(cordova)创建项目,并结合eclipse开发工具进行打包生成apk包

1.使用phonegap(cordova)创建并编辑项目 (1)创建一个文件夹用于存放稍后创建的Android程序,这里我们在E盘创建了一个文件夹AndroidProject,适用cd命令进入该目录,接下来适用phonegap命令创建对应的android项目. phonegap的创建指令:  phonegap create hello com.example.hello HelloWorld hello:你的项目文件夹名称,   com.example.hello:你的项目内部包名   Hell

cordova 基本命令 以及如何添加,删除插件

1.首先下载安装  node.js 在命令提示符 里 输入 node -v  会显示版本号证明安装成功 2.全局安装 cordova: npm install -g cordova 命令提示符里输入 cordova -v 查看版本号证明安装成功 3.创建APP cordova create hello com.example.hello HelloWorld 结果: 4.进入项目 cd hello 5.查看本机安装的平台 cordova platforms list 结果: 6.给项目添加平台支

Laravel创建项目和安装PHPStorm IDE插件

一.win10下安装composer1.下载composer.phar,放入php的安装目录https://getcomposer.org/download/1.4.2/composer.phar 2.新建 composer.bat 文件,并复制下列代码到文件中,然后执行,为了速度快,你可能要挂个代理 @php "%~dp0composer.phar" %* 3.为了保证composer的速度,使用国内镜像: composer config repo.packagist compose

使用 Eclipse 的 SVN 插件创建项目的主干/分支/标签

原文正文 读了 Mark Phippard 的博客以及<Subversion 与版本控制>之后,我了解到 分支/标签 是 SVN 很棒的特性之一.但我在使用推荐的 "trunk"."branches"以及"tags"文件夹创建我自己的目录结构的时候颇是花了一点时间.本文将分步介绍创建  Subclipse 项目然后建立一个分支.本文假定你已经安装好了 Subclipse 并建立了一个 SVN 仓库.如果你还没有,参考<集成 SV