/** * Marks the service as a foreground service. This uses reflection to figure out whether the new * APIs for marking a service as a foreground service are available. If not, it falls back to the * old {@link #setForeground(boolean)} call. * * @param service the service to put in foreground mode * @param notificationId id of the notification to show * @param notification the notification to show */ public static void setForeground( Service service, Integer notificationId, Notification notification) { final Class<?>[] startForegroundSignature = new Class[] {int.class, Notification.class}; Method startForeground = null; try { startForeground = service.getClass().getMethod("startForeground", startForegroundSignature); try { startForeground.invoke(service, new Object[] {notificationId, notification}); } catch (IllegalArgumentException e) { // Should not happen! Log.e("Could not set TriggerService to foreground mode.", e); } catch (IllegalAccessException e) { // Should not happen! Log.e("Could not set TriggerService to foreground mode.", e); } catch (InvocationTargetException e) { // Should not happen! Log.e("Could not set TriggerService to foreground mode.", e); } } catch (NoSuchMethodException e) { // Fall back on old API. service.setForeground(true); NotificationManager manager = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(notificationId, notification); } }
@Override public boolean onContextItemSelected(MenuItem item) { AdapterView.AdapterContextMenuInfo info; try { info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); } catch (ClassCastException e) { Log.e("Bad menuInfo", e); return false; } File file = (File) mAdapter.getItem(info.position); int itemId = item.getItemId(); if (itemId == MenuId.DELETE.getId()) { delete(file); return true; } else if (itemId == MenuId.RENAME.getId()) { rename(file); return true; } else if (itemId == MenuId.DECOMPILE.getId()) { if (!file.getAbsolutePath().endsWith(".pyc") && !file.isDirectory()) { // 非文件夹或者非pyc文件提示无法反编译 Crouton.cancelAllCroutons(); Crouton.showText(this, getString(R.string.s_Cannotdecompile), Style.ALERT); return true; } Decompile.show(this, file.getAbsolutePath()); return true; } else if (itemId == MenuId.BUILD.getId()) { // 打包项目 } return false; }
protected void handleNetwork() { try { if (serverSock == null) { serverSock = new ServerSocket(8080); thrd = new Thread( new Runnable() { private BufferedReader r; // private BufferedWriter out; public void run() { Log.d("Waiting for socket..."); try { sock = ScriptService.this.serverSock.accept(); Log.d( "Accepted socket " + sock.getLocalAddress() + " " + sock.getLocalPort()); r = new BufferedReader(new InputStreamReader(sock.getInputStream())); // out = new BufferedWriter(new OutputStreamWriter( // sock.getOutputStream())); while (!Thread.interrupted()) { final String data = r.readLine(); if (data != null) { // do something in ui thread with the // data var Log.d("Data: " + data); // Handle results String[] splitData = data.split("##"); if (splitData.length > 1) { if (splitData[0].equals("RESULTS")) { Log.d("Got results back: " + splitData[0]); Intent intent = new Intent(); intent.setAction(RESULT_ACTION); intent.putExtra("DATAPASSED", splitData[1]); sendBroadcast(intent); } updateNotification("Results: " + splitData[1]); } else { updateNotification(data); } } } } catch (IOException e) { } } }); thrd.start(); } } catch (IOException ioe) { Log.e(ioe.toString()); } }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.script_manager); setTitle("sl4a"); File sl4a = null; mBaseDir = new File(new GetPath().path(this) + "/sl4a/scripts/"); Log.i("mBaseDir: " + mBaseDir.getAbsolutePath()); sl4a = mBaseDir.getParentFile(); if (!sl4a.exists()) { sl4a.mkdirs(); try { FileUtils.chmod(sl4a, 0755); } catch (Exception e) { e.printStackTrace(); } } File tfile = new File(mBaseDir.getAbsolutePath() + "/template/"); if (!tfile.exists()) { tfile.mkdirs(); // 创建模版路径 } if (!FileUtils.makeDirectories(mBaseDir, 0755)) { new AlertDialog.Builder(this) .setTitle("Error") .setMessage( getString(R.string.s_Failedtocreate) + "\n" + mBaseDir + "\n" + getString(R.string.s_PleaseCheck)) .setIcon(android.R.drawable.ic_dialog_alert) .setPositiveButton("Ok", null) .show(); } mCurrentDir = mBaseDir; mPreferences = PreferenceManager.getDefaultSharedPreferences(this); mAdapter = new ScriptManagerAdapter(this); mObserver = new ScriptListObserver(); mAdapter.registerDataSetObserver(mObserver); mConfiguration = ((BaseApplication) getApplication()).getInterpreterConfiguration(); // mManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); mList = (JazzyListView) findViewById(android.R.id.list); registerForContextMenu(mList); updateAndFilterScriptList(mQuery); mList.setAdapter(mAdapter); ActivityFlinger.attachView(mList, this); ActivityFlinger.attachView(getWindow().getDecorView(), this); startService(IntentBuilders.buildTriggerServiceIntent()); UsageTrackingConfirmation.show(this); Analytics.trackActivity(this); setupJazziness(JazzyHelper.HELIX); mList.setOnItemClickListener(listItemClick); mList.setOnScrollListener(scrollListener); new CrashEmail(this, "*****@*****.**"); }
@Override public boolean onContextItemSelected(MenuItem item) { AdapterView.AdapterContextMenuInfo info; try { info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); } catch (ClassCastException e) { Log.e("Bad menuInfo", e); return false; } Trigger trigger = mAdapter.getItem(info.position); if (trigger == null) { Log.v("No trigger selected."); return false; } if (item.getItemId() == ContextMenuId.REMOVE.getId()) { mTriggerRepository.remove(trigger); } return true; }
@Override public void onDestroy() { super.onDestroy(); Log.d("onDestroy()"); Log.d("Closing socket connections"); if (mProxy != null) mProxy.shutdown(); Log.d("Script ended"); try { if (sock != null) { sock.close(); sock = null; } if (serverSock != null) { serverSock.close(); serverSock = null; } } catch (IOException e) { Log.d(e.toString()); } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String scriptName = getIntent().getStringExtra(Constants.EXTRA_SCRIPT_PATH); String interpreter = FeaturedInterpreters.getInterpreterNameForScript(scriptName); if (interpreter == null) { Log.e("Cannot find interpreter for script " + scriptName); finish(); } final Intent activityIntent = new Intent(); Intent resolveIntent = new Intent(InterpreterConstants.ACTION_DISCOVER_INTERPRETERS); resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER); resolveIntent.setType(InterpreterConstants.MIME + Script.getFileExtension(this)); List<ResolveInfo> resolveInfos = getPackageManager().queryIntentActivities(resolveIntent, 0); if (resolveInfos != null && resolveInfos.size() == 1) { ActivityInfo info = resolveInfos.get(0).activityInfo; activityIntent.setComponent(new ComponentName(info.packageName, info.name)); activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } else { final URL url = FeaturedInterpreters.getUrlForName(interpreter); activityIntent.setAction(Intent.ACTION_VIEW); activityIntent.setData(Uri.parse(url.toString())); } AlertDialog.Builder dialog = new AlertDialog.Builder(this); dialog.setTitle(String.format("%s is not installed.", interpreter)); dialog.setMessage( String.format("Do you want to download and install APK for %s ?", interpreter)); DialogInterface.OnClickListener buttonListener = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (which == DialogInterface.BUTTON_POSITIVE) { startActivity(activityIntent); } dialog.dismiss(); finish(); } }; dialog.setNegativeButton("No", buttonListener); dialog.setPositiveButton("Yes", buttonListener); dialog.show(); }
@Override public void onStart(Intent intent, final int startId) { super.onStart(intent, startId); String path = intent.getStringExtra("scriptPath"); Log.d("Script Path: " + path); mScript = new Script(path); Log.d("FileName: " + mScript.getFileName()); Log.d("FileExtension: " + mScript.getFileExtension()); handleNetwork(); Interpreter interpreter = mInterpreterConfiguration.getInterpreterForScript(mScript.getFileName()); if (interpreter == null || !interpreter.isInstalled()) { mLatch.countDown(); if (FeaturedInterpreters.isSupported(mScript.getFileName())) { Log.d("Is Supported"); Intent i = new Intent(this, DialogActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); i.putExtra(Constants.EXTRA_SCRIPT_PATH, mScript.getFileName()); startActivity(i); } else { Log.e(this, "Cannot find an interpreter for script " + mScript.getFileName()); } stopSelf(startId); return; } File script = new File(path); Log.d("Launch with proxy "); mProxy = new AndroidProxy(this, null, true); mProxy.startLocal(); mLatch.countDown(); ScriptLauncher.launchScript( script, mInterpreterConfiguration, mProxy, new Runnable() { @Override public void run() { mProxy.shutdown(); stopSelf(startId); } }); }
@Override public boolean onPrepareOptionsMenu(Menu menu) { Log.e("onPrepareOptionsMenu"); super.onPrepareOptionsMenu(menu); return true; }