Android Open Source - popcorn-android Node J S Service






From Project

Back to project page popcorn-android.

License

The source code is released under:

GNU General Public License

If you think the Android project popcorn-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.nodejs.core;
/*from ww  w .ja va2 s .co  m*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ServiceInfo;
import android.content.res.AssetManager;
import android.os.AsyncTask;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class NodeJSService extends Service {
  ScheduledExecutorService scheduler;
  IBinder mBinder = new LocalBinder();
  private static final String TAG = "nodejs-service";
  private static final String NODEJS_PATH = "backend";
  private static final String DEFAULT_PACKAGE = "backend.zip";
  
  private String mPackageName = DEFAULT_PACKAGE;
  private NodeJSTask mTask = null;

  private class NodeJSTask extends AsyncTask<String, Void, String> {
    
    Context mContext = null;
    boolean running = false;

    NodeJSTask() {
      mContext = NodeJSService.this;
    }

    @Override
    protected String doInBackground(String... params) {
      running = true;
      
      // Copy files from assets to app path
      AssetManager assets = mContext.getAssets();

      File appPath = mContext.getDir(NODEJS_PATH, Context.MODE_PRIVATE);

      
      
      
      String mainJS = params[0];
      File js = new File(appPath, NODEJS_PATH + "/" + "src" + "/" + "app" + "/" + mainJS);
      if (!js.exists()) {
        try {
          installPackage(assets, mPackageName, appPath);
          } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.e(TAG, "Error while installing script", e);
          }
        
        
        
          }
      return js.toString();
      
    }

    @Override
    protected void onPostExecute(String js) {
      if (js != null) {
        Log.d(TAG, "run :" + js);
        NodeJSCore.run(js);
        Log.d(TAG, "run end");
      } else {
        NodeJSService.this.stopSelf();
      }
      
      running = false;
    }

  }

  public static void installScripts(AssetManager assets, File targetDir,
      String basePath) throws IOException {

    String[] files = assets.list(basePath);

    if (!targetDir.exists()) {
      targetDir.mkdirs();
      
    }

    if (files.length == 0) {
      
      // basePath is a file. Copy file
      Log.d(TAG, "copy file: " + basePath);
      File targetFile = new File(targetDir, basePath);
      InputStream src = assets.open(basePath, AssetManager.ACCESS_BUFFER);
      File path = targetFile.getParentFile();

      if (!path.exists()) {
        path.mkdirs();
      }

      FileOutputStream out = new FileOutputStream(targetFile);
      try {
        byte[] buf = new byte[4096];
        int len;
  
        while ((len = src.read(buf)) > -1) {
          out.write(buf, 0, len);
        }
        
        out.flush();
      } finally {
        src.close();
        out.close();
      }

      return;
    } else {
      for (String file : files) {
        installScripts(assets, targetDir, basePath + "/" + file);
      }
    }
  }
  
  public static void installPackage(AssetManager assets, String packageName, File targetDir) throws IOException {
    
    if (!targetDir.exists()) {
      targetDir.mkdirs();
      
      
    }
    ZipInputStream zin = new ZipInputStream(assets.open(packageName));
    
    ZipEntry ze = null;
    
    try {
      while((ze = zin.getNextEntry()) != null) {
        if(ze.isDirectory()) {
          File path = new File(targetDir, ze.getName());
          
          path.mkdirs();
        } else {
          File path = new File(targetDir, ze.getName());
          FileOutputStream out = new FileOutputStream(path);
          
          Log.d(TAG, "extract " + ze.getName() + " to " + path);
          
          byte[] buf = new byte[4096];
          int len;
          
          while((len = zin.read(buf)) != -1) {
            out.write(buf, 0, len);
          }
          
          out.flush();
          out.close();
        }
      }
    } finally {    
      zin.close();
    }

    
    
        } 
    

  public void runScript(String mainJS) throws IOException {
    synchronized(this) {
      if(mTask == null) {
        mTask = new NodeJSTask();
      }
      
      if(!mTask.running) {
        mTask.execute(mainJS);
      }
    }
  }

  @Override
  public IBinder onBind(Intent intent) {
      return mBinder;
  }
  public class LocalBinder extends Binder {
      public NodeJSService getActivity() {
       return NodeJSService.this;
      }
     }

     public String getTime() {
      SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      return mDateFormat.format(new Date());
     }
    

  
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    PackageManager pm = getPackageManager();
    ComponentName component = new ComponentName(this, this.getClass());
    ServiceInfo info = null;

    Log.d(TAG, component.toString());

    try {
      info = pm.getServiceInfo(component, PackageManager.GET_META_DATA);
    } catch (NameNotFoundException e) {
      e.printStackTrace();
      return START_NOT_STICKY;
    }

    Bundle metaData = info.metaData;

    String scriptName = metaData.getString("script");
    
    if(metaData.getString("node_package") != null) {
      mPackageName = metaData.getString("node_package");
    }

    if (scriptName == null) {
      Log.e(TAG, "Script <" + scriptName
          + "> is not set as service's meta");
      return START_NOT_STICKY;
    }

    try {
      runScript(scriptName);
    } catch (IOException e) {
      e.printStackTrace();
      return START_NOT_STICKY;
    }

    return START_STICKY;
  }
  
}




Java Source Code List

com.squareup.okhttp.Address.java
com.squareup.okhttp.ConnectionPool.java
com.squareup.okhttp.Connection.java
com.squareup.okhttp.Dispatcher.java
com.squareup.okhttp.Failure.java
com.squareup.okhttp.HttpResponseCache.java
com.squareup.okhttp.Job.java
com.squareup.okhttp.MediaType.java
com.squareup.okhttp.OkAuthenticator.java
com.squareup.okhttp.OkHttpClient.java
com.squareup.okhttp.OkResponseCache.java
com.squareup.okhttp.Request.java
com.squareup.okhttp.ResponseSource.java
com.squareup.okhttp.Response.java
com.squareup.okhttp.RouteDatabase.java
com.squareup.okhttp.Route.java
com.squareup.okhttp.TunnelRequest.java
com.squareup.okhttp.internal.AbstractOutputStream.java
com.squareup.okhttp.internal.Base64.java
com.squareup.okhttp.internal.DiskLruCache.java
com.squareup.okhttp.internal.Dns.java
com.squareup.okhttp.internal.FaultRecoveringOutputStream.java
com.squareup.okhttp.internal.NamedRunnable.java
com.squareup.okhttp.internal.Platform.java
com.squareup.okhttp.internal.StrictLineReader.java
com.squareup.okhttp.internal.Util.java
com.squareup.okhttp.internal.http.AbstractHttpInputStream.java
com.squareup.okhttp.internal.http.HeaderParser.java
com.squareup.okhttp.internal.http.HttpAuthenticator.java
com.squareup.okhttp.internal.http.HttpDate.java
com.squareup.okhttp.internal.http.HttpEngine.java
com.squareup.okhttp.internal.http.HttpTransport.java
com.squareup.okhttp.internal.http.HttpURLConnectionImpl.java
com.squareup.okhttp.internal.http.HttpsEngine.java
com.squareup.okhttp.internal.http.HttpsURLConnectionImpl.java
com.squareup.okhttp.internal.http.OkResponseCacheAdapter.java
com.squareup.okhttp.internal.http.Policy.java
com.squareup.okhttp.internal.http.RawHeaders.java
com.squareup.okhttp.internal.http.RequestHeaders.java
com.squareup.okhttp.internal.http.ResponseHeaders.java
com.squareup.okhttp.internal.http.RetryableOutputStream.java
com.squareup.okhttp.internal.http.RouteSelector.java
com.squareup.okhttp.internal.http.SpdyTransport.java
com.squareup.okhttp.internal.http.Transport.java
com.squareup.okhttp.internal.http.UnknownLengthHttpInputStream.java
com.squareup.okhttp.internal.spdy.ErrorCode.java
com.squareup.okhttp.internal.spdy.FrameReader.java
com.squareup.okhttp.internal.spdy.FrameWriter.java
com.squareup.okhttp.internal.spdy.HeadersMode.java
com.squareup.okhttp.internal.spdy.Hpack.java
com.squareup.okhttp.internal.spdy.Http20Draft06.java
com.squareup.okhttp.internal.spdy.IncomingStreamHandler.java
com.squareup.okhttp.internal.spdy.NameValueBlockReader.java
com.squareup.okhttp.internal.spdy.Ping.java
com.squareup.okhttp.internal.spdy.Settings.java
com.squareup.okhttp.internal.spdy.Spdy3.java
com.squareup.okhttp.internal.spdy.SpdyConnection.java
com.squareup.okhttp.internal.spdy.SpdyStream.java
com.squareup.okhttp.internal.spdy.Variant.java
com.squareup.okhttp.internal.tls.DistinguishedNameParser.java
com.squareup.okhttp.internal.tls.OkHostnameVerifier.java
io.popcorntime.android.PopcornTime.java
io.popcorntime.frontend.BuildConfig.java
nl.xservices.plugins.SSLCertificateChecker.java
org.apache.cordova.App.java
org.apache.cordova.AuthenticationToken.java
org.apache.cordova.BuildConfig.java
org.apache.cordova.CallbackContext.java
org.apache.cordova.Config.java
org.apache.cordova.CordovaActivity.java
org.apache.cordova.CordovaArgs.java
org.apache.cordova.CordovaChromeClient.java
org.apache.cordova.CordovaInterface.java
org.apache.cordova.CordovaPlugin.java
org.apache.cordova.CordovaResourceApi.java
org.apache.cordova.CordovaWebViewClient.java
org.apache.cordova.CordovaWebView.java
org.apache.cordova.DirectoryManager.java
org.apache.cordova.DroidGap.java
org.apache.cordova.ExifHelper.java
org.apache.cordova.ExposedJsApi.java
org.apache.cordova.FileHelper.java
org.apache.cordova.IceCreamCordovaWebViewClient.java
org.apache.cordova.JSONUtils.java
org.apache.cordova.LOG.java
org.apache.cordova.LinearLayoutSoftKeyboardDetect.java
org.apache.cordova.NativeToJsMessageQueue.java
org.apache.cordova.PluginEntry.java
org.apache.cordova.PluginManager.java
org.apache.cordova.PluginResult.java
org.apache.cordova.ScrollEvent.java
org.apache.cordova.Whitelist.java
org.apache.cordova.device.Device.java
org.apache.cordova.networkinformation.NetworkManager.java
org.common.plugins.updateapp.Update.java
org.nodejs.core.NodeJSCore.java
org.nodejs.core.NodeJSService.java