diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2ac445c4..32347289 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -76,11 +76,7 @@ - - - - - + @@ -104,7 +100,8 @@ + android:exported="true" + android:screenOrientation="landscape"> diff --git a/app/src/main/cpp/PhoneDevice.cpp b/app/src/main/cpp/PhoneDevice.cpp index fbb06a05..3c20ac94 100644 --- a/app/src/main/cpp/PhoneDevice.cpp +++ b/app/src/main/cpp/PhoneDevice.cpp @@ -183,7 +183,7 @@ CPhoneDevice::CPhoneDevice(JavaVM* vm, jobject service) mReleaseWakelockMid = env->GetMethodID(classService, "releaseWakelock", "(Ljava/lang/String;)V"); mGetPowerInfoMid = env->GetMethodID(classService, "getPowerInfo", "()Ljava/lang/String;"); - mRebootMid = env->GetMethodID(classService, "reboot", "()V"); + mRebootMid = env->GetMethodID(classService, "reboot", "(I)V"); mEnableGpsMid = env->GetMethodID(classService, "enableGps", "(Z)V"); mRequestPositionMid = env->GetMethodID(classService, "requestPosition", "()Z"); @@ -328,7 +328,7 @@ bool CPhoneDevice::GetNextScheduleItem(uint32_t tsBasedZero, uint32_t scheduleTi return false; } -bool CPhoneDevice::Reboot() +bool CPhoneDevice::Reboot(int resetType) { JNIEnv* env = NULL; bool didAttachThread = false; @@ -337,7 +337,7 @@ bool CPhoneDevice::Reboot() { ALOGE("Failed to get JNI Env"); } - env->CallVoidMethod(m_javaService, mRebootMid); + env->CallVoidMethod(m_javaService, mRebootMid, resetType); if (didAttachThread) { m_vm->DetachCurrentThread(); diff --git a/app/src/main/cpp/PhoneDevice.h b/app/src/main/cpp/PhoneDevice.h index a5de9c54..32fda5be 100644 --- a/app/src/main/cpp/PhoneDevice.h +++ b/app/src/main/cpp/PhoneDevice.h @@ -46,7 +46,7 @@ public: virtual bool UpdateTime(time_t ts); virtual bool UpdateSchedules(); virtual bool QuerySystemProperties(map& properties); - virtual bool Reboot(); + virtual bool Reboot(int resetType); virtual bool EnableGPS(bool enabled); virtual bool RequestPosition(); virtual timer_uid_t RegisterHeartbeat(unsigned int timerType, unsigned int timeout); diff --git a/app/src/main/java/com/xypower/mpapp/MainActivity.java b/app/src/main/java/com/xypower/mpapp/MainActivity.java index 0f038fd3..b3b82f1e 100644 --- a/app/src/main/java/com/xypower/mpapp/MainActivity.java +++ b/app/src/main/java/com/xypower/mpapp/MainActivity.java @@ -96,13 +96,14 @@ public class MainActivity extends AppCompatActivity { Log.d(TAG, "Screen Size: " + width + " x " + height); Intent intent = getIntent(); + final int noDelay = intent.getIntExtra("noDelay", 0); int rebootFlag = intent.getIntExtra("reboot", 0); if (rebootFlag == 1) { // SysApi.enableAirPlane(MainActivity.this, true); } - String cmdid = "0123456789ABCDEFG"; - String server = "47.96.238.157"; + String cmdid = ""; + String server = ""; Integer port = new Integer(6891); Integer protocol = new Integer(DEFAULT_PROTOCOL); // 0xFF00 @@ -148,6 +149,9 @@ public class MainActivity extends AppCompatActivity { AppConfig curAppConfig = retrieveAndSaveAppConfig(); + if (TextUtils.isEmpty(appConfig.cmdid) || TextUtils.isEmpty(appConfig.server) || appConfig.port == 0) { + return; + } Intent intent = new Intent(MainActivity.this, MicroPhotoService.class); intent.setAction(MicroPhotoService.ACTION_START); intent.putExtra("cmdid", curAppConfig.cmdid); @@ -294,7 +298,7 @@ public class MainActivity extends AppCompatActivity { } } }; - handler.postDelayed(runnable, 5000); + handler.postDelayed(runnable, noDelay != 0 ? 100 : 5000); } binding.tcpudp.setOnClickListener(new View.OnClickListener() { @@ -374,7 +378,8 @@ public class MainActivity extends AppCompatActivity { appConfig.cmdid = MainActivity.this.binding.cmdid.getText().toString(); appConfig.server = MainActivity.this.binding.server.getText().toString(); - appConfig.port = Integer.parseInt(MainActivity.this.binding.port.getText().toString()); + String portStr = MainActivity.this.binding.port.getText().toString(); + appConfig.port = TextUtils.isEmpty(portStr) ? 0 : Integer.parseInt(portStr); String protocolStr = MainActivity.this.binding.protocol.getSelectedItem().toString(); appConfig.protocol = DEFAULT_PROTOCOL; String[] parts = protocolStr.split("-"); @@ -437,6 +442,7 @@ public class MainActivity extends AppCompatActivity { private void saveAppConfig(String cmdid, String server, int port, int protocol, int networkProtocol) { String appPath = MicroPhotoService.buildAppDir(this.getApplicationContext()); + InputStreamReader inputStreamReader = null; OutputStreamWriter outputStreamWriter = null; try { @@ -445,7 +451,17 @@ public class MainActivity extends AppCompatActivity { dataPath.mkdirs(); } - JSONObject jsonObject = new JSONObject(); + inputStreamReader = new InputStreamReader(new FileInputStream(new File(appPath + "data/App.json")), "UTF-8"); + BufferedReader bufferedReader = new BufferedReader(inputStreamReader); + String line; + StringBuilder stringBuilder = new StringBuilder(); + while ((line = bufferedReader.readLine()) != null) { + stringBuilder.append(line); + } + bufferedReader.close(); + + JSONObject jsonObject = new JSONObject(stringBuilder.toString()); + jsonObject.put("CMDID", cmdid); jsonObject.put("Server", server); jsonObject.put("Port", port); @@ -461,6 +477,12 @@ public class MainActivity extends AppCompatActivity { } catch (JSONException e) { e.printStackTrace(); } finally { + if (inputStreamReader != null) { + try { + inputStreamReader.close(); + } catch (Exception ex) { + } + } if (outputStreamWriter != null) { try { outputStreamWriter.close(); diff --git a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java index 79b895aa..b571a8cc 100644 --- a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java +++ b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java @@ -1,5 +1,6 @@ package com.xypower.mpapp; +import android.app.ActivityManager; import android.app.AlarmManager; import android.app.Notification; import android.app.NotificationChannel; @@ -19,6 +20,8 @@ import android.net.wifi.WifiManager; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; +import android.os.Message; +import android.os.Messenger; import android.os.PowerManager; import android.os.SystemClock; @@ -79,6 +82,8 @@ public class MicroPhotoService extends Service { } private static int mStateService = STATE_SERVICE.NOT_CONNECTED; + public final static int MSG_WHAT_NETWORK_CHANGE = 10; + private String mCmdid = ""; private NotificationManager mNotificationManager; private final Map mWakeLocks = new HashMap<>(); @@ -88,11 +93,14 @@ public class MicroPhotoService extends Service { private PositionManager mPositionManager = null; private final Map mTimers = new HashMap<>(); - protected long mHandler = 0; + protected long mNativeHandle = 0; private AlarmReceiver mAlarmReceiver = null; private ScreenActionReceiver mScreenaAtionReceiver = null; private NetworkChangedReceiver mNetworkChangedReceiver = null; + private ServiceHandler mHander = null; + private Messenger mMessenger = null; + public MicroPhotoService() { } @Override @@ -104,12 +112,14 @@ public class MicroPhotoService extends Service { public void onCreate() { super.onCreate(); + mHander = new ServiceHandler(); + mMessenger = new Messenger(mHander); + mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); mStateService = STATE_SERVICE.NOT_CONNECTED; DeviceUtil.getPhoneState(this.getApplicationContext()); - mScreenaAtionReceiver = new ScreenActionReceiver(); // 注册广播接受者 @@ -122,7 +132,7 @@ public class MicroPhotoService extends Service { registerReceiver(mAlarmReceiver, intentFilter); } { - mNetworkChangedReceiver = new NetworkChangedReceiver(); + mNetworkChangedReceiver = new NetworkChangedReceiver(this); IntentFilter filter = new IntentFilter(); filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); @@ -157,8 +167,8 @@ public class MicroPhotoService extends Service { mStateService = STATE_SERVICE.NOT_CONNECTED; - uninit(mHandler); - mHandler = 0; + uninit(mNativeHandle); + mNativeHandle = 0; unregisterReceiver(mAlarmReceiver); unregisterReceiver(mScreenaAtionReceiver); @@ -172,6 +182,21 @@ public class MicroPhotoService extends Service { super.onDestroy(); } + public static class ServiceHandler extends Handler { + @Override + public void dispatchMessage(Message msg) { + super.dispatchMessage(msg); + // Log.i("life", "MyHandler----dispatchMessage"); + // Log.i("life", Thread.currentThread().getName()); + } + + @Override + public void handleMessage(Message msg) { + super.handleMessage(msg); + // Log.i("life", "MyHandler----handleMessage"); + } + } + public String getCmdid() { return mCmdid; } @@ -188,7 +213,7 @@ public class MicroPhotoService extends Service { String action = intent.getAction(); if (TextUtils.equals(ACTION_HEARTBEAT, action)) { Log.i(TAG, "HB Timer Fired ACTION=" + action); - mService.sendHeartbeat(mService.mHandler); + mService.sendHeartbeat(mService.mNativeHandle); mService.registerHeartbeatTimer(); String cmdid = mService.getCmdid(); @@ -209,7 +234,7 @@ public class MicroPhotoService extends Service { int preset = (int) ((val & 0xFF00L) >> 8); Log.i(TAG, "PhotoTimer Fired: CH=" + channel + " PR=" + preset); - mService.notifyToTakePhoto(mService.mHandler, channel, preset, ts, mService.buildPhotoDir(mService.getApplicationContext(), channel), mService.buildPhotoFileName(channel, preset, ts), true); + mService.notifyToTakePhoto(mService.mNativeHandle, channel, preset, ts, mService.buildPhotoDir(mService.getApplicationContext(), channel), mService.buildPhotoFileName(channel, preset, ts), true); } } @@ -231,7 +256,7 @@ public class MicroPhotoService extends Service { long ts = System.currentTimeMillis() / 1000; Log.i(TAG, "Take Photo CH=" + channel + " PR=" + preset + " Mannually"); - mService.notifyToTakePhoto(mService.mHandler, channel, preset, ts, mService.buildPhotoDir(mService.getApplicationContext(), channel), mService.buildPhotoFileName(channel, preset, ts), photoOrVideo); + mService.notifyToTakePhoto(mService.mNativeHandle, channel, preset, ts, mService.buildPhotoDir(mService.getApplicationContext(), channel), mService.buildPhotoFileName(channel, preset, ts), photoOrVideo); } else if (TextUtils.equals(ACTION_TIMEOUT, action)) { long uid = intent.getLongExtra(EXTRA_PARAM_TIMER_UID, 0); long expectedTimes = intent.getLongExtra(EXTRA_PARAM_TIMES, 0); @@ -239,7 +264,7 @@ public class MicroPhotoService extends Service { elapsedTimes++; Log.i(TAG, "Timeout uid=" + uid + " expectedTimes=" + expectedTimes + " Times=" + elapsedTimes); - mService.fireTimeout(mService.mHandler, uid, elapsedTimes); + mService.fireTimeout(mService.mNativeHandle, uid, elapsedTimes); intent.putExtra(EXTRA_PARAM_ELASPED_TIMES, elapsedTimes); @@ -379,7 +404,7 @@ public class MicroPhotoService extends Service { } private boolean registerCaptureSchedule(long startTime, long baseTime) { - long[] photoTimeData = getPhotoTimeData(mHandler); + long[] photoTimeData = getPhotoTimeData(mNativeHandle); if (photoTimeData == null) { return false; } @@ -483,13 +508,13 @@ public class MicroPhotoService extends Service { int protocol = intent.getIntExtra("protocol", 0); int networkProtocol = intent.getIntExtra("networkProtocol", 0); - Log.i(TAG, "AppPath=" + appPath + " Server=" + ip + ":" + port + " cmdid=" + cmdid + " protocol" + protocol + " Network=" + networkProtocol); - mHandler = init(appPath, ip, port, cmdid, protocol, networkProtocol); + Log.i(TAG, "AppPath=" + appPath + " Server=" + ip + ":" + port + " cmdid=" + cmdid + " protocol=" + protocol + " Network=" + networkProtocol); - // Start the locker receiver + MicroPhotoService service = MicroPhotoService.this; + service.mNativeHandle = init(appPath, ip, port, cmdid, protocol, networkProtocol); - if (mHandler !=0) { - mCmdid = cmdid; + if (service.mNativeHandle !=0) { + service.mCmdid = cmdid; Date date = new Date(); long nowTs = date.getTime() / 1000; date.setHours(0); @@ -498,7 +523,7 @@ public class MicroPhotoService extends Service { long startTime = date.getTime() / 1000; long baseTime = nowTs - startTime; - registerCaptureSchedule(startTime, baseTime); + service.registerCaptureSchedule(startTime, baseTime); // AppMaster appMaster = new AppMaster(this, cmdid); // appMaster.start(); @@ -552,7 +577,7 @@ public class MicroPhotoService extends Service { private void connect() { // after 10 seconds its connected - new android.os.Handler().postDelayed( + mHander.postDelayed( new Runnable() { public void run() { Log.d(TAG, "Bluetooth Low Energy device is connected!!"); @@ -699,7 +724,7 @@ public class MicroPhotoService extends Service { Location location = mLocationManager.getLastKnownLocation(mLocateType); if (location != null) { - updatePosition(mHandler, location.getLongitude(), location.getLatitude(), location.getTime()); + updatePosition(mNativeHandle, location.getLongitude(), location.getLatitude(), location.getTime()); } // Set Listener mLocationManager.requestLocationUpdates(mLocateType, 100,0, locationListener); @@ -750,16 +775,35 @@ public class MicroPhotoService extends Service { return sb.toString(); } - public void reboot() { + public void reboot(final int rebootType) { - Handler handler = new Handler(); Runnable runnable = new Runnable() { @Override public void run() { - SysApi.reboot(getApplicationContext()); + if (rebootType == 0) { + /* + Context context = MicroPhotoService.this.getApplicationContext(); + Intent intent = getPackageManager().getLaunchIntentForPackage(context.getPackageName()); + + int noDelay = 1; + intent.putExtra("noDelay", noDelay); + PendingIntent restartIntent = PendingIntent.getActivity(context, 0, intent, 0); + AlarmManager mgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE); + mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, restartIntent); // 1秒钟后重启应用 + System.exit(0); + + */ + Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage(getApplication().getPackageName()); + LaunchIntent.putExtra("noDelay", 1); + LaunchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + startActivity(LaunchIntent); + + } else { + SysApi.reboot(getApplicationContext()); + } } }; - handler.postDelayed(runnable, 1000); + mHander.postDelayed(runnable, 1000); } public void enableGps(boolean enabled) { SysApi.enableGps(getApplicationContext(), enabled); @@ -797,7 +841,7 @@ cellSignalStrengthGsm.getDbm(); @Override public void onLocationChanged(Location location) { - updatePosition(mHandler, location.getLongitude(), location.getLatitude(), location.getTime()); + updatePosition(mNativeHandle, location.getLongitude(), location.getLatitude(), location.getTime()); // Log.i(TAG, "Time: " + location.getTime()); // Log.i(TAG, "经度:" + location.getLongitude()); // Log.i(TAG, "纬度:" + location.getLatitude()); diff --git a/app/src/main/java/com/xypower/mpapp/NetworkChangedReceiver.java b/app/src/main/java/com/xypower/mpapp/NetworkChangedReceiver.java index face00f4..42140c7e 100644 --- a/app/src/main/java/com/xypower/mpapp/NetworkChangedReceiver.java +++ b/app/src/main/java/com/xypower/mpapp/NetworkChangedReceiver.java @@ -6,10 +6,20 @@ import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.wifi.WifiManager; +import android.os.Message; import android.os.Parcelable; import android.util.Log; +import java.lang.ref.WeakReference; + public class NetworkChangedReceiver extends BroadcastReceiver { + + private WeakReference mService; + + public NetworkChangedReceiver(MicroPhotoService service) { + mService = new WeakReference<>(service); + } + private String getConnectionType(int type) { String connType = ""; if (type == ConnectivityManager.TYPE_MOBILE) { @@ -56,14 +66,18 @@ public class NetworkChangedReceiver extends BroadcastReceiver { NetworkInfo info = intent .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO); if (info != null) { - //如果当前的网络连接成功并且网络连接可用 - if (NetworkInfo.State.CONNECTED == info.getState() && info.isAvailable()) { - if (info.getType() == ConnectivityManager.TYPE_WIFI - || info.getType() == ConnectivityManager.TYPE_MOBILE) { - Log.i("TAG", getConnectionType(info.getType()) + "连上"); + // If connected and available + MicroPhotoService service = mService.get(); + if (service != null) { + if (NetworkInfo.State.CONNECTED == info.getState() && info.isAvailable()) { + if (info.getType() == ConnectivityManager.TYPE_WIFI + || info.getType() == ConnectivityManager.TYPE_MOBILE) { + // Log.i("TAG", getConnectionType(info.getType()) + "连上"); + + } + } else { + Log.i("TAG", getConnectionType(info.getType()) + "断开"); } - } else { - Log.i("TAG", getConnectionType(info.getType()) + "断开"); } } } diff --git a/app/src/main/res/layout-land/activity_main.xml b/app/src/main/res/layout-land/activity_main.xml new file mode 100644 index 00000000..a4c365e8 --- /dev/null +++ b/app/src/main/res/layout-land/activity_main.xml @@ -0,0 +1,264 @@ + + + + + + + + + + + + + + + + + +