diff --git a/build.gradle b/build.gradle index b0f12a8b..fd16d8e4 100644 --- a/build.gradle +++ b/build.gradle @@ -10,8 +10,8 @@ buildscript { } project.ext { - versionCode = 130 - versionName = "1.2.8" + versionCode = 131 + versionName = "1.2.9" } task hello { diff --git a/phonk_app/src/main/assets/changelog.txt b/phonk_app/src/main/assets/changelog.txt index 8ff26d33..2a572a66 100644 --- a/phonk_app/src/main/assets/changelog.txt +++ b/phonk_app/src/main/assets/changelog.txt @@ -1,3 +1,12 @@ +1.2.9 +- Toggle state order are fixed +- Camera methods changed to fix callbacks +- MQTT methods are changed a bit to work better asynchronously. Hopefully it won't change anymore! :) +- Background service now stops properly +- Fixed memory leak in main app +- Custom icons in projects and shortcuts +- Scripts now launch with the same previous orientation. This is nice for tablets! + 1.2.8 - Decimals method in slider and knob now works - Launch other scripts via app.launchScript(path) method diff --git a/phonk_app/src/main/java/io/phonk/MainActivity.java b/phonk_app/src/main/java/io/phonk/MainActivity.java index 06875ead..5e95c52d 100644 --- a/phonk_app/src/main/java/io/phonk/MainActivity.java +++ b/phonk_app/src/main/java/io/phonk/MainActivity.java @@ -30,6 +30,7 @@ import android.content.res.Configuration; import android.net.ConnectivityManager; import android.os.Bundle; +import android.os.Handler; import android.os.PersistableBundle; import android.view.ContextThemeWrapper; import android.view.MenuItem; @@ -82,7 +83,7 @@ public class MainActivity extends BaseActivity { private static final java.lang.String TAG = MainActivity.class.getSimpleName(); - private AppRunnerCustom mAppRunner; + // private AppRunnerCustom mAppRunner; private Intent mServerIntent; @@ -107,10 +108,12 @@ public class MainActivity extends BaseActivity { private boolean isWebIdeMode = false; private boolean mUiInit = false; - private PDelay mDelay; + // private PDelay mDelay; private boolean alreadyStartedServices = false; private boolean isConfigChanging = false; + private Runnable mDelay; + private Handler mHandler; @Override protected void onCreate(Bundle savedInstanceState) { @@ -126,8 +129,8 @@ protected void onCreate(Bundle savedInstanceState) { alreadyStartedServices = savedInstanceState.getBoolean("alreadyStartedServices", false); } - mAppRunner = new AppRunnerCustom(this); - mAppRunner.initDefaultObjects(AppRunnerHelper.createSettings()).initInterpreter(); + // mAppRunner = new AppRunnerCustom(this); + // mAppRunner.initDefaultObjects(AppRunnerHelper.createSettings()).initInterpreter(); // PhonkApp phonkApp = new PhonkApp(mAppRunner); // phonkApp.network.checkVersion(); @@ -141,7 +144,18 @@ protected void onCreate(Bundle savedInstanceState) { loadUI(1); } else { loadUI(0); - mDelay = mAppRunner.pUtil.delay(3000, () -> mViewPager.setCurrentItem(1)); + + mHandler = new Handler(); + mDelay = new Runnable() { + @Override + public void run() { + mViewPager.setCurrentItem(1); + mHandler.removeCallbacks(this); + } + }; + mHandler.postDelayed(mDelay, 3000); + + // mDelay = mAppRunner.pUtil.delay(3000, () -> mViewPager.setCurrentItem(1)); } setScreenAlwaysOn((boolean) UserPreferences.getInstance().get("screen_always_on")); @@ -181,8 +195,9 @@ protected void onPause() { protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); - if (mDelay != null) mDelay.stop(); - mAppRunner.byebye(); + // if (mDelay != null) mDelay.stop(); + if (mHandler != null) mHandler.removeCallbacks(mDelay); + // mAppRunner.byebye(); if (!isConfigChanging) stopServers(); } @@ -289,7 +304,8 @@ private void loadUI(int toPage) { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { if (positionOffset > 0.1) { - if (mDelay != null) mDelay.stop(); + // if (mDelay != null) mDelay.stop(); + if (mHandler != null) mHandler.removeCallbacks(mDelay); } if (position == 0) { diff --git a/phonk_app/src/main/java/io/phonk/gui/ConnectionInfoFragment.java b/phonk_app/src/main/java/io/phonk/gui/ConnectionInfoFragment.java index 35f407d1..f63f7311 100644 --- a/phonk_app/src/main/java/io/phonk/gui/ConnectionInfoFragment.java +++ b/phonk_app/src/main/java/io/phonk/gui/ConnectionInfoFragment.java @@ -237,7 +237,6 @@ public void addTextToConsole(String text) { handler.post(() -> mComputerText.append(text + "\n> ")); } - // folder choose @Subscribe public void onEventMainThread(Events.FolderChosen e) { diff --git a/phonk_app/src/main/java/io/phonk/gui/projectlist/ProjectItem.java b/phonk_app/src/main/java/io/phonk/gui/projectlist/ProjectItem.java index 12ae6a97..e08904d6 100644 --- a/phonk_app/src/main/java/io/phonk/gui/projectlist/ProjectItem.java +++ b/phonk_app/src/main/java/io/phonk/gui/projectlist/ProjectItem.java @@ -72,7 +72,6 @@ public class ProjectItem extends LinearLayout { public ProjectItem(Context context, boolean listMode) { super(context); this.mContext = context; - //this.mPlf = plf; LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); if (listMode) { @@ -86,17 +85,8 @@ public ProjectItem(Context context, boolean listMode) { customIcon = mItemView.findViewById(R.id.iconImg); this.setOnClickListener(v -> { - /* - AnimatorSet animSpin; - animSpin = (AnimatorSet) AnimatorInflater.loadAnimator(v.getContext(), R.animator.run); - animSpin.setTarget(v); - animSpin.start(); - */ - Runnable r = () -> { PhonkAppHelper.launchScript(getContext(), mProject); - // getActivity().overridePendingTransition(R.anim.splash_slide_in_anim_set, - // R.anim.splash_slide_out_anim_set); }; Handler handler = new Handler(); @@ -116,41 +106,6 @@ public void setText(String text) { textViewName.setText(text); } - public void reInit(String text, boolean selected) { - setText(text); - // TODO reenable this setHighlighted(selected); - } - - public void drawText(ImageView imageView, String t2) { - - // ImageView myImageView = - Bitmap myBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.RGB_565); - Paint myPaint = new Paint(); - myPaint.setColor(Color.BLUE); - myPaint.setAntiAlias(true); - myPaint.setTextSize(80); - - int x1 = 10; - int y1 = 80; - int x2 = 20; - int y2 = 20; - - // Create mContext new image bitmap and attach a brand new canvas to it - Bitmap tempBitmap = Bitmap.createBitmap(myBitmap.getWidth(), myBitmap.getHeight(), Bitmap.Config.RGB_565); - Canvas tempCanvas = new Canvas(tempBitmap); - - // Draw the image bitmap into the cavas - tempCanvas.drawBitmap(myBitmap, 0, 0, null); - - // Draw everything else you want into the canvas, in this example mContext - // rectangle with rounded edges - tempCanvas.drawRoundRect(new RectF(x1, y1, x2, y2), 2, 2, myPaint); - tempCanvas.drawText(t2.substring(0, 1).toUpperCase(), x1, y1, myPaint); - - // Attach the canvas to the ImageView - imageView.setImageDrawable(new BitmapDrawable(getResources(), tempBitmap)); - } - public void setMenu() { // setting menu for mProject.getName()); mMenuButton = findViewById(R.id.card_menu_button); @@ -161,15 +116,7 @@ public void setMenu() { return true; }); - //customIcon.setOnCreateContextMenuListener(); mMenuButton.setOnClickListener(v -> showMenu(mMenuButton)); -// this.setOnLongClickListener(new OnLongClickListener() { -// @Override -// public boolean onLongClick(View v) { -// showContextMenu(); -// return true; -// } -// }); } private void showMenu(View fromView) { @@ -177,18 +124,14 @@ private void showMenu(View fromView) { PopupMenu myPopup = new PopupMenu(wrapper, fromView); myPopup.inflate(R.menu.project_actions); myPopup.setOnMenuItemClickListener(menuItem -> { - int itemId = menuItem.getItemId(); switch (itemId) { case R.id.menu_project_list_run: - // EventBus.getDefault().post(new Events.ProjectEvent(Events.PROJECT_RUN, mProject)); PhonkAppHelper.launchScript(getContext(), mProject); return true; case R.id.menu_project_list_edit: PhonkAppHelper.launchEditor(getContext(), mProject); - - // EventBus.getDefault().post(new Events.ProjectEvent(Events.PROJECT_EDIT, mProject)); return true; case R.id.menu_project_webeditor: PhonkAppHelper.openInWebEditor(getContext(), mProject); @@ -233,21 +176,6 @@ private void showMenu(View fromView) { } - /* - public Drawable getBg() { - return bg; - } - - public void setHighlighted(boolean highlighted) { - if (highlighted) { - getBg().setColorFilter(0x22000000, PorterDuff.Mode.MULTIPLY); - } else { - getBg().clearColorFilter(); - } - this.highlighted = highlighted; - } - */ - public boolean isHighlighted() { return highlighted; } @@ -263,16 +191,8 @@ public void setProjectInfo(Project p) { setText(p.getName()); setTag(p.getName()); - File f = new File(p.getFullPathForFile("icon.png")); - // setImage(R.drawable.primarycolor_rounded_rect); - - if (f.exists()) { - BitmapFactory.Options bmOptions = new BitmapFactory.Options(); - Bitmap bitmap = BitmapFactory.decodeFile(f.getPath(), bmOptions); - bitmap = Bitmap.createScaledBitmap(bitmap, 100, 100, true); - - setImage(bitmap); - } + Bitmap icon = p.getIcon(); + if (icon != null) setImage(icon); setMenu(); } diff --git a/phonk_app/src/main/java/io/phonk/gui/projectlist/ProjectItemAdapter.java b/phonk_app/src/main/java/io/phonk/gui/projectlist/ProjectItemAdapter.java index 348a3bdb..ed7b3047 100644 --- a/phonk_app/src/main/java/io/phonk/gui/projectlist/ProjectItemAdapter.java +++ b/phonk_app/src/main/java/io/phonk/gui/projectlist/ProjectItemAdapter.java @@ -89,10 +89,9 @@ public int findAppPosByName(String appName) { for (int i = 0; i < mProjectList.size(); i++) { String name = mProjectList.get(i).getName(); - // MLog.d(TAG, "name " + name); if (name.equals(appName)) { - pos = i; //(int) mProjectAdapter.getItemId(i); + pos = i; break; } diff --git a/phonk_app/src/main/java/io/phonk/helpers/PhonkScriptHelper.java b/phonk_app/src/main/java/io/phonk/helpers/PhonkScriptHelper.java index b85082a9..33f6385f 100644 --- a/phonk_app/src/main/java/io/phonk/helpers/PhonkScriptHelper.java +++ b/phonk_app/src/main/java/io/phonk/helpers/PhonkScriptHelper.java @@ -26,6 +26,7 @@ import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; +import android.graphics.Bitmap; import android.net.Uri; import android.os.Environment; @@ -428,9 +429,6 @@ public static String fileExt(String url) { public static void addShortcut(Context c, String folder, String name) { Project p = new Project(folder, name); - Intent.ShortcutIconResource icon; - icon = Intent.ShortcutIconResource.fromContext(c, R.drawable.app_icon); - if (ShortcutManagerCompat.isRequestPinShortcutSupported(c)) { Intent shortcutIntent = new Intent(c, AppRunnerActivity.class); shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); @@ -439,10 +437,15 @@ public static void addShortcut(Context c, String folder, String name) { shortcutIntent.putExtra(Project.FOLDER, p.getFolder()); shortcutIntent.setAction(Intent.ACTION_MAIN); + Bitmap bitmapIcon = p.getIcon(); + + IconCompat icon = IconCompat.createWithResource(c, R.drawable.app_icon); + if (bitmapIcon != null) icon = IconCompat.createWithBitmap(bitmapIcon); + ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(c, folder + "/" + name) .setIntent(shortcutIntent) // !!! intent's action must be set on oreo .setShortLabel(name) - .setIcon(IconCompat.createWithResource(c, R.drawable.app_icon)) + .setIcon(icon) .build(); ShortcutManagerCompat.requestPinShortcut(c, shortcutInfo, null); } diff --git a/phonk_app/src/main/java/io/phonk/server/PhonkServerService.java b/phonk_app/src/main/java/io/phonk/server/PhonkServerService.java index 9d247d15..272df745 100644 --- a/phonk_app/src/main/java/io/phonk/server/PhonkServerService.java +++ b/phonk_app/src/main/java/io/phonk/server/PhonkServerService.java @@ -159,19 +159,6 @@ public void onCreate() { mEventsProxy = new EventsProxy(); EventBus.getDefault().register(this); - /* - // go back to app intent - Intent resultIntent = new Intent(this, MainActivity.class); - - // The stack object will contain an artificial back stack for - // navigating backward from the Activity leads out your application to the Home screen. - TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); - stackBuilder.addParentStack(MainActivity.class); - stackBuilder.addNextIntent(resultIntent); - - PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); - */ - // close server intent Intent notificationIntent = new Intent(this, PhonkServerService.class).setAction(SERVICE_CLOSE); PendingIntent pendingIntentStopService = PendingIntent.getService(this, (int) System.currentTimeMillis(), notificationIntent, 0); @@ -377,7 +364,7 @@ public void onDestroy() { unregisterReceiver(stopActivitiyBroadcastReceiver); unregisterReceiver(viewsUpdateBroadcastReceiver); - fileObserver.stopWatching(); + if (fileObserver != null) fileObserver.stopWatching(); EventBus.getDefault().unregister(this); } @@ -478,7 +465,6 @@ public void onReceive(Context context, Intent intent) { } }; - @Subscribe public void onEventMainThread(Events.ProjectEvent e) { MLog.d(TAG, "connect -> " + e.getAction()); diff --git a/phonk_apprunner/src/main/java/io/phonk/runner/AppRunnerActivity.java b/phonk_apprunner/src/main/java/io/phonk/runner/AppRunnerActivity.java index 06bb4be5..0cf0e253 100644 --- a/phonk_apprunner/src/main/java/io/phonk/runner/AppRunnerActivity.java +++ b/phonk_apprunner/src/main/java/io/phonk/runner/AppRunnerActivity.java @@ -111,7 +111,6 @@ public class AppRunnerActivity extends BaseActivity { private boolean debugFramentIsVisible; private boolean orientationChanged = false; private Bundle mBundle; - private boolean isLandscape; private boolean isPortrait; private Map scriptSettings; @@ -154,15 +153,16 @@ protected void onCreate(Bundle savedInstanceState) { */ Point size = new Point(); getWindowManager().getDefaultDisplay().getSize(size); - if (size.x > size.y) isLandscape = true; + if (size.x > size.y) isPortrait = false; else isPortrait = true; - if (orientation.equals("landscape")) { // && !isLandscape) { + if (orientation.equals("landscape")) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); - // orientationChanged = true; - } else if (orientation.equals("portrait")) { // && !isPortrait) { + } else if (orientation.equals("portrait")) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); - // orientationChanged = true; + } else if (orientation.equals("current")) { + if (isPortrait) setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); + else setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } // if we changed the orientation, we will wait for the orientation to change diff --git a/phonk_apprunner/src/main/java/io/phonk/runner/AppRunnerFragment.java b/phonk_apprunner/src/main/java/io/phonk/runner/AppRunnerFragment.java index 80d3d3ce..893b5a23 100644 --- a/phonk_apprunner/src/main/java/io/phonk/runner/AppRunnerFragment.java +++ b/phonk_apprunner/src/main/java/io/phonk/runner/AppRunnerFragment.java @@ -171,7 +171,7 @@ public void onDestroy() { super.onDestroy(); MLog.d(TAG, "onDestroy"); if (mAppRunner.interp != null) mAppRunner.interp.callJsFunction("onDestroy"); - // fileObserver.stopWatching(); + if (fileObserver != null) fileObserver.stopWatching(); mAppRunner.byebye(); } diff --git a/phonk_apprunner/src/main/java/io/phonk/runner/AppRunnerService.java b/phonk_apprunner/src/main/java/io/phonk/runner/AppRunnerService.java index 77d14ada..cded5291 100644 --- a/phonk_apprunner/src/main/java/io/phonk/runner/AppRunnerService.java +++ b/phonk_apprunner/src/main/java/io/phonk/runner/AppRunnerService.java @@ -23,6 +23,7 @@ package io.phonk.runner; import android.app.AlarmManager; +import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; @@ -55,6 +56,7 @@ import io.phonk.runner.apprunner.interpreter.AppRunnerInterpreter; import io.phonk.runner.base.events.Events; import io.phonk.runner.base.models.Project; +import io.phonk.runner.base.network.NetworkUtils; import io.phonk.runner.base.utils.MLog; public class AppRunnerService extends Service { @@ -70,7 +72,6 @@ public class AppRunnerService extends Service { private RelativeLayout parentScriptedLayout; private RelativeLayout mainLayout; - private BroadcastReceiver mReceiver; private NotificationManager mNotifManager; private PendingIntent mRestartPendingIntent; private Toast mToast; @@ -79,14 +80,31 @@ public class AppRunnerService extends Service { private boolean mOverlayIsEnabled = false; private NotificationManager mNotificationManager; private int mNotificationId; + private NotificationCompat.Builder mNotificationBuilder; + private NotificationChannel mChannel; + private String mNotificationChannelId = "phonk_script"; + private CharSequence mNotificationText = "Notification text"; + + @Override + public void onCreate() { + super.onCreate(); + MLog.d(TAG, "onCreate"); + } @Override public int onStartCommand(Intent intent, int flags, int startId) { - registerEventBus(); + MLog.d(TAG, "onStartCommand " + intent); - mainLayout = initLayout(); + if (intent.getAction() != null) { + if (intent.getAction().equals(SERVICE_CLOSE)) { + stopSelf(); + return START_STICKY; + } + } mContext = this; + registerEventBus(); + mainLayout = initLayout(); AppRunnerSettings.SERVER_PORT = intent.getIntExtra(Project.SERVER_PORT, 0); @@ -138,25 +156,25 @@ public int onStartCommand(Intent intent, int flags, int startId) { startStopActivityBroadcastReceiver(); executeCodeActivityBroadcastReceiver(); + mToast = Toast.makeText(AppRunnerService.this, "Service crashed :(", Toast.LENGTH_LONG); + + // catch errors and send them to the WebIDE or the app console + AppRunnerInterpreter.InterpreterInfo appRunnerCb = (resultType, message) -> mAppRunner.pConsole.p_error(resultType, message); + mAppRunner.initProject(); + // notification mNotifManager = (NotificationManager) AppRunnerService.this.getSystemService(Context.NOTIFICATION_SERVICE); mNotificationId = (int) Math.ceil(100000 * Math.random()); createNotification(mNotificationId, mAppRunner.getProject().getFolder(), mAppRunner.getProject().getName()); - //just in case it crash + // just in case it crash Intent restartIntent = new Intent("io.phonk.LauncherActivity"); //getApplicationContext(), AppRunnerActivity.class); restartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); restartIntent.putExtra("wasCrash", true); mRestartPendingIntent = PendingIntent.getActivity(AppRunnerService.this, 0, restartIntent, 0); - mToast = Toast.makeText(AppRunnerService.this, "Service crashed :(", Toast.LENGTH_LONG); - // catch errors and send them to the WebIDE or the app console - AppRunnerInterpreter.InterpreterInfo appRunnerCb = (resultType, message) -> mAppRunner.pConsole.p_error(resultType, message); - // mAppRunner.interp.addListener(appRunnerCb); - - mAppRunner.initProject(); - // mAppRunner.interp.callJsFunction("onCreate"); + startForeground(mNotificationId, mNotificationBuilder.build()); return Service.START_NOT_STICKY; } @@ -174,24 +192,10 @@ public void unregisterEventBus() { } private void createNotification(final int notificationId, String scriptFolder, String scriptName) { - IntentFilter filter = new IntentFilter(); - filter.addAction(SERVICE_CLOSE); - - mReceiver = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - if (intent.getAction().equals(SERVICE_CLOSE)) { - AppRunnerService.this.stopSelf(); - mNotifManager.cancel(notificationId); - } - } - }; - - registerReceiver(mReceiver, filter); - //RemoteViews remoteViews = new RemoteViews(getPackageName(), // R.layout.widget); + /* Intent stopIntent = new Intent(SERVICE_CLOSE); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, stopIntent, 0); @@ -216,10 +220,39 @@ public void onReceive(Context context, Intent intent) { mBuilder.setContentIntent(resultPendingIntent); mNotificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE); mNotificationManager.notify(notificationId, mBuilder.build()); - Thread.setDefaultUncaughtExceptionHandler(handler); + */ - } + // close server intent + Intent notificationIntent = new Intent(this, AppRunnerService.class).setAction(SERVICE_CLOSE); + PendingIntent pendingIntentStopService = PendingIntent.getService(this, (int) System.currentTimeMillis(), notificationIntent, 0); + mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); + + mNotificationBuilder = new NotificationCompat.Builder(this, mNotificationChannelId) + .setSmallIcon(R.drawable.dotted) + // .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) + .setContentTitle(this.getString(R.string.app_name)) + .setContentText("Running " + scriptName) + .setOngoing(false) + .setColor(this.getResources().getColor(R.color.phonk_colorPrimary)) + // .setContentIntent(pendingIntent) + // .setOnlyAlertOnce(true) + .addAction(R.drawable.ic_action_stop, "Stop script", pendingIntentStopService); + // .setContentInfo("1 Connection"); + // mNotificationBuilder.build().flags = Notification.FLAG_ONGOING_EVENT; + + // damm annoying android pofkjpodsjf0ewiah + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + int importance = NotificationManager.IMPORTANCE_LOW; + mChannel = new NotificationChannel(mNotificationChannelId, this.getString(R.string.app_name), importance); + // mChannel.setDescription("lalalla"); + mChannel.enableLights(false); + mNotificationManager.createNotificationChannel(mChannel); + } else { + } + + Thread.setDefaultUncaughtExceptionHandler(handler); + } Thread.UncaughtExceptionHandler handler = new Thread.UncaughtExceptionHandler() { @Override @@ -275,19 +308,10 @@ public IBinder onBind(Intent intent) { return null; } - @Override - public void onCreate() { - super.onCreate(); - MLog.d(TAG, "onCreate"); - } - @Override public void onDestroy() { super.onDestroy(); MLog.d(TAG, "onDestroy"); - - // mAppRunner.interp.callJsFunction("onDestroy"); - if (mOverlayIsEnabled) windowManager.removeView(mainLayout); Intent i = new Intent("io.phonk.intent.CLOSED"); @@ -295,7 +319,6 @@ public void onDestroy() { unregisterReceiver(stopActivitiyBroadcastReceiver); unregisterReceiver(executeCodeActivitiyBroadcastReceiver); mNotificationManager.cancel(mNotificationId); - unregisterReceiver(mReceiver); unregisterEventBus(); mAppRunner.byebye(); // mAppRunner.interp = null; diff --git a/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/AppRunnerHelper.java b/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/AppRunnerHelper.java index 8a538035..a8f66d33 100644 --- a/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/AppRunnerHelper.java +++ b/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/AppRunnerHelper.java @@ -88,7 +88,7 @@ private static void resetValues(Map map) { if (!map.containsKey("description")) map.put("description", "portrait"); if (!map.containsKey("icon")) map.put("icon", "portrait"); if (!map.containsKey("window_size")) map.put("window_size", ""); - if (!map.containsKey("orientation")) map.put("orientation", "portrait"); + if (!map.containsKey("orientation")) map.put("orientation", "current"); if (!map.containsKey("screen_mode")) map.put("screen_mode", "normal"); if (!map.containsKey("featured_image")) map.put("featured_image", ""); if (!map.containsKey("background_service")) map.put("background_service", false); diff --git a/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/PDevice.java b/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/PDevice.java index 98b43d82..e0778ab4 100644 --- a/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/PDevice.java +++ b/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/PDevice.java @@ -905,9 +905,9 @@ private void turnFlashlightOff() { @Override public void __stop() { - getContext().unregisterReceiver(batteryReceiver); - getContext().unregisterReceiver(onNotification); - getContext().unregisterReceiver(smsReceiver); + if (batteryReceiver != null) getContext().unregisterReceiver(batteryReceiver); + if (onNotification != null) getContext().unregisterReceiver(onNotification); + if (smsReceiver != null) getContext().unregisterReceiver(smsReceiver); batteryReceiver = null; onNotification = null; diff --git a/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/PFileIO.java b/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/PFileIO.java index dd809890..a36596c1 100644 --- a/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/PFileIO.java +++ b/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/PFileIO.java @@ -213,7 +213,6 @@ public void appendTextToFile(String line, String fileName) { FileIO.appendString(getAppRunner().getProject().getFullPathForFile(fileName), line); } - @PhonkMethod(description = "Append an array of text into a file", example = "") @PhonkMethodParam(params = {"fileName", "lines[]"}) public void appendTextToFileAsync(final String line, final String fileName, final ReturnInterface callback) { @@ -223,7 +222,6 @@ public void appendTextToFileAsync(final String line, final String fileName, fina }); } - @PhonkMethod(description = "Load the Strings of a text file into an array", example = "") @PhonkMethodParam(params = {"fileName"}) public String loadTextFromFile(String fileName) { @@ -248,13 +246,13 @@ public Typeface loadFont(String fontName) { } @PhonkMethod(description = "Loads a bitmap", example = "") - public Bitmap loadBitmap(String path) { + public Bitmap loadImage(String path) { return Image.loadBitmap(getAppRunner().getProject().getFullPathForFile(path)); } - public void loadBitmapAsync(final String path, final ReturnInterface callback) { + public void loadImageAsync(final String path, final ReturnInterface callback) { Thread t = new Thread(() -> { - loadBitmap(path); + loadImage(path); returnValues(null, callback); }); t.start(); @@ -380,7 +378,7 @@ public void saveImage(Bitmap finalBitmap, String fileName, String type, int qual if (file.exists()) file.delete (); try { FileOutputStream out = new FileOutputStream(file); - finalBitmap.compress(formatType, 100, out); + finalBitmap.compress(formatType, quality, out); out.flush(); out.close(); } catch (Exception e) { diff --git a/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/PMedia.java b/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/PMedia.java index fc317d77..a86ec38a 100644 --- a/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/PMedia.java +++ b/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/PMedia.java @@ -70,6 +70,7 @@ import io.phonk.runner.apprunner.api.media.PTextToSpeech; import io.phonk.runner.apprunner.api.media.PVideo; import io.phonk.runner.apprunner.api.media.PWave; +import io.phonk.runner.apprunner.interpreter.AppRunnerInterpreter; import io.phonk.runner.apprunner.interpreter.PhonkNativeArray; import io.phonk.runner.base.utils.AndroidUtils; import io.phonk.runner.base.utils.MLog; @@ -380,14 +381,25 @@ public void scanQRcode(byte[] data, Camera camera) { * @advanced */ public PCamera useCamera(String camera) { - PCamera pCamera = new PCamera(getAppRunner(), camera); + boolean existsCameraFront = (boolean) getAppRunner().pDevice.info().get("cameraFront"); + boolean existsCameraBack = (boolean) getAppRunner().pDevice.info().get("cameraBack"); + + boolean cameraIsAvailable = camera.equals("front") & existsCameraFront | camera.equals("back") & existsCameraBack; + // MLog.d("qq", existsCameraFront + " " + existsCameraBack + " " + cameraIsAvailable); + + if (!cameraIsAvailable) { + // MLog.d("qq", "no camera"); + // getAppRunner().pConsole.p_error(AppRunnerInterpreter.RESULT_NOT_CAPABLE, camera + " camera"); + return null; + } + PCamera pCamera = new PCamera(getAppRunner(), camera); return pCamera; } - + @Override public void __stop() { - getContext().unregisterReceiver(headsetPluggedReceiver); + if (headsetCallbackfn != null) getContext().unregisterReceiver(headsetPluggedReceiver); } } diff --git a/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/PViewsArea.java b/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/PViewsArea.java index 2a24c7ee..58530654 100644 --- a/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/PViewsArea.java +++ b/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/PViewsArea.java @@ -457,9 +457,9 @@ public PToggle addToggle(final String[] text, Object x, Object y, Object w, Obje map.put("textOn", text[0]); map.put("textOff", text[0]); } else if (text.length == 2) { - map.put("text", text[1]); - map.put("textOn", text[0]); - map.put("textOff", text[1]); + map.put("text", text[0]); + map.put("textOn", text[1]); + map.put("textOff", text[0]); } else if (text.length == 3) { map.put("text", text[0]); map.put("textOn", text[1]); diff --git a/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/media/PCamera.java b/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/media/PCamera.java index 0226385c..c00b89f3 100644 --- a/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/media/PCamera.java +++ b/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/media/PCamera.java @@ -25,14 +25,12 @@ import android.hardware.Camera; import java.util.List; -import java.util.Map; import io.phonk.runner.apidoc.annotation.PhonkClass; import io.phonk.runner.apidoc.annotation.PhonkMethod; import io.phonk.runner.apidoc.annotation.PhonkMethodParam; import io.phonk.runner.apprunner.AppRunner; import io.phonk.runner.apprunner.api.common.ReturnInterface; -import io.phonk.runner.apprunner.api.widgets.PViewMethodsInterface; import io.phonk.runner.apprunner.api.widgets.StylePropertiesProxy; import io.phonk.runner.apprunner.api.widgets.Styler; import io.phonk.runner.base.gui.CameraTexture; @@ -44,7 +42,6 @@ public class PCamera extends CameraTexture implements /* PViewMethodsInterface,* private LearnImages learnImages = null; private DetectImage detectImage = null; - private ReturnInterface mOnPictureTakenCallback; // this is a props proxy for the user public StylePropertiesProxy props = new StylePropertiesProxy(); @@ -63,27 +60,26 @@ public PCamera(AppRunner appRunner, String camera) { }); } + @PhonkMethod public PCamera onPictureTaken(final ReturnInterface callbackfn) { - mOnPictureTakenCallback = callbackfn; + addPictureTakenCallback(callbackfn); + return this; } - @PhonkMethodParam(params = {"fileName"}) + @PhonkMethod + public PCamera onVideoTaken(final ReturnInterface callbackfn) { + addVideoTakenCallback(callbackfn); + + return this; + } + + + @PhonkMethod(description = "Takes a picture and saves it to fileName", example = "camera.takePicture();") // @APIRequires() - public void takePicture(String file) { - takePic(mAppRunner.getProject().getFullPathForFile(file)); - addListener(new CameraListener() { - @Override - public void onVideoRecorded() {} - - @Override - public void onPicTaken() { - mOnPictureTakenCallback.event(null); - cam.removeListener(this); - } - }); - + public void takePicture() { + takePic(); } public List getSizes() { @@ -168,44 +164,32 @@ public void onNewFrameBase64(CameraTexture.CallbackStream callbackfn) { @PhonkMethodParam(params = {"width", "height"}) @PhonkMethod(description = "Set the camera preview resolution", example = "") - public void setPreviewSize(int w, int h) { + public void previewSize(int w, int h) { super.setPreviewSize(w, h); } @PhonkMethodParam(params = {"width", "height"}) @PhonkMethod(description = "Set the camera picture resolution", example = "") - public void setPictureResolution(int w, int h) { + public void pictureResolution(int w, int h) { super.setPictureSize(w, h); } @PhonkMethodParam(params = {"{'none', 'mono', 'sepia', 'negative', 'solarize', 'posterize', 'whiteboard', 'blackboard'}"}) @PhonkMethod(description = "Set the camera picture effect if supported", example = "") - public void setColorEffect(String effect) { + public void colorEffect(String effect) { super.setColorEffect(effect); } @PhonkMethod(description = "Records a video in fileName", example = "") @PhonkMethodParam(params = {"fileName"}) - public void recordVideo(String file, final ReturnInterface callbackfn) { - recordVideo(mAppRunner.getProject().getFullPathForFile(file)); - addListener(new CameraListener() { - - @Override - public void onVideoRecorded() { - callbackfn.event(null); - cam.removeListener(this); - } - - @Override - public void onPicTaken() { - } - }); + public void recordVideo(String file) { + super.recordVideo(mAppRunner.getProject().getFullPathForFile(file)); } @PhonkMethod(description = "Stops recording the video", example = "") @PhonkMethodParam(params = {""}) public void stopRecordingVideo() { - stopRecordingVideo(); + super.stopRecordingVideo(); } @PhonkMethod(description = "Checks if flash is available", example = "") @@ -216,7 +200,7 @@ public boolean isFlashAvailable() { @PhonkMethod(description = "Turns on/off the flash", example = "") @PhonkMethodParam(params = {""}) - public void flash(boolean b) { + public void flashLight(boolean b) { super.flash(b); } diff --git a/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/media/PCamera2.java b/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/media/PCamera2.java index abcea9fa..8b7dc01e 100644 --- a/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/media/PCamera2.java +++ b/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/media/PCamera2.java @@ -49,7 +49,7 @@ public PCamera2(AppRunner appRunner) { } @Override - public void takePicture(String file) { + public void takePicture() { } @@ -69,12 +69,12 @@ public void focus(ReturnInterface callback) { } @Override - public void setPreviewSize(int w, int h) { + public void previewSize(int w, int h) { } @Override - public void setPictureResolution(int w, int h) { + public void pictureResolution(int w, int h) { } @@ -103,7 +103,7 @@ public static void startFlash(Context c, boolean b) { } @Override - public void setColorEffect(String effect) { + public void colorEffect(String effect) { } diff --git a/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/media/PCameraInterface.java b/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/media/PCameraInterface.java index a872ea26..f55132a6 100644 --- a/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/media/PCameraInterface.java +++ b/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/media/PCameraInterface.java @@ -28,7 +28,7 @@ public interface PCameraInterface { - void takePicture(String file); + void takePicture(); void recordVideo(String file); @@ -36,15 +36,15 @@ public interface PCameraInterface { void focus(ReturnInterface callback); - void setPreviewSize(int w, int h); + void previewSize(int w, int h); - void setPictureResolution(int w, int h); + void pictureResolution(int w, int h); boolean isFlashAvailable(); void flash(boolean b); - void setColorEffect(String effect); + void colorEffect(String effect); void onNewFrame(final CameraTexture.CallbackData callbackfn); diff --git a/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/network/PBluetoothLE.java b/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/network/PBluetoothLE.java index ab4bfe8b..50c61806 100644 --- a/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/network/PBluetoothLE.java +++ b/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/network/PBluetoothLE.java @@ -113,7 +113,7 @@ public PBluetoothLE scan(final ReturnInterface callbackfn) { } public PBluetoothLE stopScan() { - mBleAdapter.stopLeScan(mScanCallback); + if (mBleAdapter != null) mBleAdapter.stopLeScan(mScanCallback); return this; } diff --git a/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/network/PMqtt.java b/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/network/PMqtt.java index 4ab29cbe..acfa55db 100644 --- a/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/network/PMqtt.java +++ b/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/network/PMqtt.java @@ -21,10 +21,11 @@ */ package io.phonk.runner.apprunner.api.network; +import org.eclipse.paho.client.mqttv3.IMqttActionListener; import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; +import org.eclipse.paho.client.mqttv3.IMqttToken; import org.eclipse.paho.client.mqttv3.MqttAsyncClient; import org.eclipse.paho.client.mqttv3.MqttCallbackExtended; -import org.eclipse.paho.client.mqttv3.MqttClient; import org.eclipse.paho.client.mqttv3.MqttConnectOptions; import org.eclipse.paho.client.mqttv3.MqttException; import org.eclipse.paho.client.mqttv3.MqttMessage; @@ -45,7 +46,9 @@ public class PMqtt extends ProtoBase { private final String TAG = PMqtt.class.getSimpleName(); private MqttAsyncClient client; - private ReturnInterface mCallback; + private ReturnInterface mCallbackData; + private ReturnInterface mCallbackConnected; + private ReturnInterface mCallbackDisconnected; public PMqtt(AppRunner appRunner) { super(appRunner); @@ -76,29 +79,47 @@ public PMqtt connect(Map connectionSettings) { connOpts.setPassword(((String)connectionSettings.get("password")).toCharArray()); } + if (connectionSettings.containsKey("autoConnect")) { + connOpts.setAutomaticReconnect(((boolean) connectionSettings.get("autoConnect"))); + } + connOpts.setCleanSession(true); - client.connect(connOpts); + client.connect(connOpts, "", new IMqttActionListener() { + @Override + public void onSuccess(IMqttToken asyncActionToken) { + ReturnObject ret = new ReturnObject(); + ret.put("status", "connected"); + + mHandler.post(() -> { + if (mCallbackConnected != null) mCallbackConnected.event(ret); + }); + MLog.d(TAG, "connectComplete"); + } - client.setCallback(new MqttCallbackExtended() { @Override - public void connectComplete(boolean reconnect, String serverURI) { + public void onFailure(IMqttToken asyncActionToken, Throwable exception) { ReturnObject ret = new ReturnObject(); - ret.put("status", "connectComplete"); - ret.put("broker", serverURI); + ret.put("status", "failure"); mHandler.post(() -> { - mCallback.event(ret); + if (mCallbackConnected != null) mCallbackConnected.event(ret); }); MLog.d(TAG, "connectComplete"); } + }); + + client.setCallback(new MqttCallbackExtended() { + @Override + public void connectComplete(boolean reconnect, String serverURI) { + } @Override public void connectionLost(Throwable cause) { - MLog.d(TAG, "connectionLost"); + MLog.d(TAG, "disconnected"); ReturnObject ret = new ReturnObject(); - ret.put("status", "connectionLost"); + ret.put("status", "disconnected"); mHandler.post(() -> { - mCallback.event(ret); + if (mCallbackDisconnected != null) mCallbackDisconnected.event(ret); }); } @@ -114,7 +135,7 @@ public void messageArrived(String topic, MqttMessage message) throws Exception { ret.put("id", message.getId()); ret.put("topic", topic); mHandler.post(() -> { - mCallback.event(ret); + mCallbackData.event(ret); }); } @@ -124,7 +145,7 @@ public void deliveryComplete(IMqttDeliveryToken token) { ReturnObject ret = new ReturnObject(); ret.put("status", "deliveryComplete"); mHandler.post(() -> { - mCallback.event(ret); + mCallbackData.event(ret); }); } }); @@ -184,7 +205,27 @@ public PMqtt unsubscribe(final String topic) { * @return */ public PMqtt onNewData(ReturnInterface callback) { - mCallback = callback; + mCallbackData = callback; + return this; + } + + /** + * Callback that returns connection status + * @param callback + * @return + */ + public PMqtt onConnected(ReturnInterface callback) { + mCallbackConnected = callback; + return this; + } + + /** + * Callback that returns connection status + * @param callback + * @return + */ + public PMqtt onDisconnected(ReturnInterface callback) { + mCallbackDisconnected = callback; return this; } diff --git a/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/other/WhatIsRunning.java b/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/other/WhatIsRunning.java index a8cea09e..82f671fc 100644 --- a/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/other/WhatIsRunning.java +++ b/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/other/WhatIsRunning.java @@ -31,19 +31,14 @@ public class WhatIsRunning { private static final String TAG = WhatIsRunning.class.getSimpleName(); - private WhatIsRunning instance; private Vector runners; public WhatIsRunning() { runners = new Vector(); + // MLog.d(TAG, "instancing WhatIsRunning..."); } public void stopAll() { - MLog.d("qqbyebye", "stopping"); - for (Object o : runners) { - // MLog.d(TAG, "list " + o.getClass().getCanonicalName()); - } - for (Object o : runners) { Method method = null; @@ -53,20 +48,26 @@ public void stopAll() { } catch (NoSuchMethodException e) { } - MLog.d("qqbyebye", "stopping " + o.getClass().getCanonicalName() + " " + o + " " + method); + MLog.d(TAG, "stopping " + o.getClass().getCanonicalName() + " " + o + " " + method); try { - //if (method !== null) { method.invoke(o); - //} } catch (IllegalArgumentException e) { + MLog.d(TAG, "cannot stop 1"); } catch (IllegalAccessException e) { + MLog.d(TAG, "cannot stop 2"); } catch (InvocationTargetException e) { + MLog.d(TAG, "cannot stop 3"); + e.printStackTrace(); + } catch (Exception e) { + MLog.d(TAG, "cannot stop 4"); + e.printStackTrace(); } } } public void add(Object object) { + // MLog.d(TAG, "adding: " + object.getClass().getCanonicalName()); runners.add(object); } diff --git a/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/sensors/PLocation.java b/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/sensors/PLocation.java index 57f04527..7a7af330 100644 --- a/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/sensors/PLocation.java +++ b/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/sensors/PLocation.java @@ -331,7 +331,7 @@ public boolean isAvailable() { public void stop() { running = false; - locationManager.removeUpdates(locationListener); + if (locationManager != null) locationManager.removeUpdates(locationListener); } @Override diff --git a/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/widgets/StylePropertiesProxy.java b/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/widgets/StylePropertiesProxy.java index 140d27b6..a586a373 100644 --- a/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/widgets/StylePropertiesProxy.java +++ b/phonk_apprunner/src/main/java/io/phonk/runner/apprunner/api/widgets/StylePropertiesProxy.java @@ -168,7 +168,7 @@ public Object get(Object key) { @Override public Object put(String key, Object value) { - MLog.d(TAG, "put 3: " + key + " " + values.get(key)); + // MLog.d(TAG, "put 3: " + key + " " + values.get(key)); values.put(key, value); return value; @@ -181,7 +181,7 @@ public Object remove(Object key) { @Override public void putAll(Map m) { - MLog.d(TAG, "putAll: "); + // MLog.d(TAG, "putAll: "); values.putAll(m); } diff --git a/phonk_apprunner/src/main/java/io/phonk/runner/base/gui/CameraTexture.java b/phonk_apprunner/src/main/java/io/phonk/runner/base/gui/CameraTexture.java index ebd0153b..c5877986 100644 --- a/phonk_apprunner/src/main/java/io/phonk/runner/base/gui/CameraTexture.java +++ b/phonk_apprunner/src/main/java/io/phonk/runner/base/gui/CameraTexture.java @@ -63,6 +63,7 @@ import io.phonk.runner.apprunner.AppRunner; import io.phonk.runner.apprunner.api.common.ReturnInterface; +import io.phonk.runner.apprunner.api.common.ReturnObject; import io.phonk.runner.apprunner.api.media.AutoFitTextureView; import io.phonk.runner.apprunner.interpreter.AppRunnerInterpreter; import io.phonk.runner.base.utils.MLog; @@ -92,18 +93,14 @@ public class CameraTexture extends AutoFitTextureView implements TextureView.Sur private String _path; private View v; - private Vector listeners = new Vector(); private CallbackData callbackData; private CallbackBmp callbackBmp; private CallbackStream callbackStream; private boolean frameProcessing = false; protected Parameters mParameters; private OnReadyCallback mOnReadyCallback; - - public interface CameraListener { - void onPicTaken(); - void onVideoRecorded(); - } + private ReturnInterface mPictureTakenCallback; + private ReturnInterface mVideoTakenCallback; public CameraTexture(AppRunner appRunner, String camera, String colorMode) { super(appRunner.getAppContext()); @@ -306,25 +303,19 @@ protected void stopCamera() { File file = null; String fileName; - public String takePic(final String path) { + public String takePic() { // final CountDownLatch latch = new CountDownLatch(1); AudioManager mgr = (AudioManager) mAppRunner.getAppContext().getSystemService(Context.AUDIO_SERVICE); mgr.setStreamMute(AudioManager.STREAM_SYSTEM, true); - SoundPool soundPool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 0); - // final int shutterSound = soundPool.load(this, R.raw.camera_click, 0); mCamera.takePicture(null, null, (data, camera) -> { - Bitmap bitmapPicture = BitmapFactory.decodeByteArray(data, 0, data.length); - - // soundPool.play(shutterSound, 1f, 1f, 0, 0, 1); - + /* FileOutputStream outStream = null; try { - file = new File(path); outStream = new FileOutputStream(file); @@ -332,20 +323,18 @@ public String takePic(final String path) { outStream.flush(); outStream.close(); MLog.d(TAG, "onPictureTaken - wrote bytes: " + data.length); - - for (CameraListener l : listeners) { - l.onPicTaken(); - } - } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { } + */ + ReturnObject ret = new ReturnObject(); + ret.put("bitmap", bitmapPicture); + if (mPictureTakenCallback != null) mPictureTakenCallback.event(ret); MLog.d(TAG, "onPictureTaken - jpeg"); - camera.startPreview(); }); @@ -364,7 +353,6 @@ public void stopRecordingVideo() { } public void recordVideo(String file) { - MLog.d(TAG, "mCamera " + mCamera); // Camera.Parameters parameters = mCamera.getParameters(); // MLog.d(TAG, "parameters " + parameters); @@ -511,22 +499,19 @@ public boolean hasAutofocus() { public void focus(final ReturnInterface callbackfn) { if (hasAutofocus()) { - if (true) { - mCamera.autoFocus((success, camera) -> { - if (callbackfn != null) callbackfn.event(null); - }); - } else { - mCamera.cancelAutoFocus(); - } + mCamera.autoFocus((success, camera) -> { + if (callbackfn != null) callbackfn.event(null); + }); + mCamera.cancelAutoFocus(); } } - public void addListener(CameraListener listener) { - listeners.add(listener); + public void addPictureTakenCallback(ReturnInterface callback) { + mPictureTakenCallback = callback; } - public void removeListener(CameraListener listener) { - listeners.remove(listener); + public void addVideoTakenCallback(ReturnInterface callback) { + mVideoTakenCallback = callback; } public void setCameraDisplayOrientation(int cameraId, android.hardware.Camera camera) { diff --git a/phonk_apprunner/src/main/java/io/phonk/runner/base/gui/DebugFragment.java b/phonk_apprunner/src/main/java/io/phonk/runner/base/gui/DebugFragment.java index 03521b53..381a4d13 100644 --- a/phonk_apprunner/src/main/java/io/phonk/runner/base/gui/DebugFragment.java +++ b/phonk_apprunner/src/main/java/io/phonk/runner/base/gui/DebugFragment.java @@ -142,6 +142,7 @@ public boolean onTouchEvent(MotionEvent event) { @Subscribe(sticky = true) public void onEventMainThread(Events.LogEvent e) { String logMsg = e.getData(); + MLog.d("qq logMsg", logMsg); int actionType = AppRunnerInterpreter.RESULT_OK; if (e.getAction() == "log_error") actionType = AppRunnerInterpreter.RESULT_ERROR; diff --git a/phonk_apprunner/src/main/java/io/phonk/runner/base/models/Project.java b/phonk_apprunner/src/main/java/io/phonk/runner/base/models/Project.java index 56938965..c87c36f7 100644 --- a/phonk_apprunner/src/main/java/io/phonk/runner/base/models/Project.java +++ b/phonk_apprunner/src/main/java/io/phonk/runner/base/models/Project.java @@ -22,6 +22,9 @@ package io.phonk.runner.base.models; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; + import java.io.File; import io.phonk.runner.apprunner.AppRunnerSettings; @@ -114,4 +117,24 @@ public boolean exists() { File f = new File(getFullPath()); return f.exists(); } + + public String getIconUrl() { + File f = new File(this.getFullPathForFile("icon.png")); + if (f.exists()) return f.toString(); + else return null; + } + + public Bitmap getIcon() { + String iconUrl = getIconUrl(); + + if (iconUrl != null) { + BitmapFactory.Options bmOptions = new BitmapFactory.Options(); + Bitmap bitmap = BitmapFactory.decodeFile(iconUrl, bmOptions); + bitmap = Bitmap.createScaledBitmap(bitmap, 100, 100, true); + return bitmap; + } else { + return null; + } + + } } diff --git a/phonk_apprunner/src/main/java/io/phonk/runner/base/services/NotificationService.java b/phonk_apprunner/src/main/java/io/phonk/runner/base/services/NotificationService.java index b1ccda4e..9dfc66f3 100644 --- a/phonk_apprunner/src/main/java/io/phonk/runner/base/services/NotificationService.java +++ b/phonk_apprunner/src/main/java/io/phonk/runner/base/services/NotificationService.java @@ -23,17 +23,24 @@ package io.phonk.runner.base.services; import android.annotation.TargetApi; +import android.app.ActivityManager; +import android.content.ComponentName; import android.content.Context; import android.content.Intent; +import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; +import android.os.IBinder; import android.service.notification.NotificationListenerService; import android.service.notification.StatusBarNotification; import android.util.Log; import androidx.localbroadcastmanager.content.LocalBroadcastManager; +import java.util.List; + import io.phonk.runner.base.utils.AndroidUtils; +import io.phonk.runner.base.utils.MLog; @TargetApi(Build.VERSION_CODES.KITKAT) public class NotificationService extends NotificationListenerService { @@ -47,6 +54,7 @@ public void onCreate() { @Override public void onNotificationPosted(StatusBarNotification sbn) { + MLog.d("notification", "received"); if (AndroidUtils.isVersionKitKat()) { String pack = sbn.getPackageName(); @@ -91,4 +99,16 @@ private String getString(CharSequence c) { public void onNotificationRemoved(StatusBarNotification sbn) { Log.i("Msg", "Notification Removed"); } + + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + return START_STICKY; + } + + + @Override + public IBinder onBind(Intent intent) { + return null; + } } \ No newline at end of file diff --git a/phonk_apprunner/src/main/java/io/phonk/runner/base/utils/FileIO.java b/phonk_apprunner/src/main/java/io/phonk/runner/base/utils/FileIO.java index 2a506562..e4fc893c 100644 --- a/phonk_apprunner/src/main/java/io/phonk/runner/base/utils/FileIO.java +++ b/phonk_apprunner/src/main/java/io/phonk/runner/base/utils/FileIO.java @@ -59,7 +59,6 @@ import io.phonk.runner.apprunner.AppRunnerSettings; public class FileIO { - private static final String TAG = FileIO.class.getSimpleName(); /** @@ -246,7 +245,6 @@ private static void copyFile(Context c, String filename) { OutputStream out = null; try { in = assetManager.open(filename); - //TODO reenable this String newFileName = AppRunnerSettings.getBaseDir() + filename; out = new FileOutputStream(newFileName); @@ -263,7 +261,6 @@ private static void copyFile(Context c, String filename) { } catch (Exception e) { Log.e("tag", e.getMessage()); } - } public static void copyFile(File src, File dst) throws IOException { @@ -511,7 +508,6 @@ public static HashMap> seeZipContent2(String path) { } static public void extractZip(String zipFile, String location) throws IOException { - int size; int BUFFER_SIZE = 1024; @@ -572,7 +568,6 @@ public static File[] listFiles(String path, final String extension) { File f = new File(path); return f.listFiles((dir, fileName) -> fileName.endsWith(extension)); - } public static String getFileExtension(String fileName) {