Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.54 KB | None | 0 0
  1. package px.ks1702;
  2.  
  3. import android.annotation.SuppressLint;
  4. import android.app.Activity;
  5. import android.app.ActivityManager;
  6.  
  7. import android.app.admin.DevicePolicyManager;
  8. import android.app.admin.SystemUpdatePolicy;
  9. import android.content.BroadcastReceiver;
  10. import android.content.ComponentName;
  11. import android.content.Context;
  12. import android.content.Intent;
  13. import android.content.IntentFilter;
  14. import android.content.SharedPreferences;
  15. import android.content.pm.ActivityInfo;
  16. import android.content.pm.PackageManager;
  17. import android.graphics.PixelFormat;
  18. import android.net.Uri;
  19. import android.os.AsyncTask;
  20. import android.os.BatteryManager;
  21. import android.os.UserManager;
  22. import android.provider.Settings;
  23. import android.support.v7.app.AppCompatActivity;
  24. import android.os.Bundle;
  25. import android.util.Log;
  26. import android.view.View;
  27. import android.view.WindowManager;
  28. import android.webkit.WebChromeClient;
  29. import android.webkit.WebSettings;
  30. import android.webkit.WebView;
  31. import android.webkit.WebViewClient;
  32. import android.widget.Button;
  33. import android.widget.FrameLayout;
  34. import android.widget.MediaController;
  35. import android.widget.TextView;
  36. import android.widget.Toast;
  37. import android.widget.VideoView;
  38.  
  39. import com.google.firebase.iid.FirebaseInstanceId;
  40. import com.google.firebase.messaging.FirebaseMessaging;
  41. import com.google.firebase.messaging.RemoteMessage;
  42.  
  43. import org.apache.commons.net.ftp.FTPClient;
  44. import org.w3c.dom.Text;
  45.  
  46. import java.io.File;
  47. import java.io.FileInputStream;
  48. import java.io.FileOutputStream;
  49. import java.io.IOException;
  50. import java.io.InputStream;
  51. import java.io.OutputStream;
  52. import java.net.NetworkInterface;
  53. import java.util.Collections;
  54. import java.util.HashMap;
  55. import java.util.List;
  56. import java.util.Map;
  57. import java.util.Random;
  58. import java.util.Timer;
  59. import java.util.TimerTask;
  60.  
  61. public class MainActivity extends Activity {
  62. private static final String LOG_TAG = "MainActivity";
  63.  
  64. private WebView mWebView;
  65. private TextView databaseTextView;
  66. private Button unlockButton;
  67.  
  68. //Lockdown
  69. private DevicePolicyManager mDevicePolicyManager;
  70. private PackageManager mPackageManager;
  71. private ComponentName mAdminComponentName;
  72.  
  73. // listens for when FTP settings are initialised
  74. private BroadcastReceiver ftpSettingsReceiver = new BroadcastReceiver() {
  75. @Override
  76. public void onReceive(Context context, Intent intent) {
  77. TextView textview = (TextView) findViewById(R.id.database_textview);
  78. Bundle bundle = intent.getBundleExtra("msg");
  79. // Log.e(LOG_TAG, mWebView.getUrl());
  80.  
  81. if (mWebView.getUrl().equals("file:///android_asset/www/loading.html"))
  82. {
  83. textview.setText("Database ID: " + bundle.getString("databaseId"));
  84. saveConfig();
  85. }
  86. }
  87. };
  88.  
  89. // listens for when the screen orientation is updated and display it on the screen
  90. private BroadcastReceiver screenOrientationReceiver = new BroadcastReceiver() {
  91. @Override
  92. public void onReceive(Context context, Intent intent) {
  93. saveConfig();
  94. setScreenOrientation();
  95. }
  96. };
  97.  
  98. // listens for when the screen orientation is updated and display it on the screen
  99. private BroadcastReceiver lockdownReceiver = new BroadcastReceiver() {
  100. @Override
  101. public void onReceive(Context context, Intent intent) {
  102. Bundle bundle = intent.getBundleExtra("msg");
  103. String lockdown = bundle.getString("lockdown");
  104.  
  105. if (lockdown.equals("yes")) {
  106. Log.d(LOG_TAG, "Lockdown message recieved, initiating");
  107. // setupLockdown();
  108. // startLockdown();
  109. }
  110. else if (lockdown.equals("no")) {
  111. Log.d(LOG_TAG, "Removing lockdown");
  112.  
  113. // stopLockdown();
  114. }
  115. }
  116. };
  117.  
  118.  
  119.  
  120. // listens for when the client and screen ID is updated
  121. private BroadcastReceiver clientAndScreenIdReceiver = new BroadcastReceiver() {
  122. @Override
  123. public void onReceive(Context context, Intent intent) {
  124. saveConfig();
  125. // determine if there is a internet connection
  126. // if there is a connection start downloading files.
  127.  
  128. TextView textview = (TextView) findViewById(R.id.database_textview);
  129.  
  130. // Append a -R to text_view to signal that the application has RECIEVED
  131. // a message to change its client and screen id
  132.  
  133. if (! textview.getText().equals("") )
  134. {
  135. textview.setText(textview.getText() + "-R");
  136. }
  137. // TODO Need to put check for update command here. So that when the IDs change they automatically refresh
  138.  
  139. // TODO Uncomment to work with Admin Page and Firebase
  140. DetermineConnection();
  141. }
  142. };
  143.  
  144. // listens for when reload screen is called
  145. private BroadcastReceiver reloadScreenReceiver = new BroadcastReceiver() {
  146. @Override
  147. public void onReceive(Context context, Intent intent) {
  148. mWebView.reload();
  149. }
  150. };
  151.  
  152. // listens for when force update is called
  153. private BroadcastReceiver forceUpdateReceiver = new BroadcastReceiver() {
  154. @Override
  155. public void onReceive(Context context, Intent intent) {
  156. Bundle bundle = intent.getBundleExtra("msg");
  157.  
  158. FtpDownloader FTP = new FtpDownloader(context, mWebView, databaseTextView);
  159.  
  160. if (bundle.getString("delete").equals("yes"))
  161. {
  162. Log.d(LOG_TAG, "Deleting..." + Util.CLIENT_ID);
  163. FTP.Delete(Util.CLIENT_ID);
  164. }
  165.  
  166. Log.d(LOG_TAG, "Updating...");
  167.  
  168. FTP.execute(Util.FTP_SERVER, Util.FTP_USERNAME, Util.FTP_PASSWORD, Util.CLIENT_ID + "/" + Util.SCREEN_ID);
  169. }
  170. };
  171.  
  172.  
  173.  
  174. @Override
  175. protected void onCreate(Bundle savedInstanceState) {
  176. super.onCreate(savedInstanceState);
  177. setContentView(R.layout.activity_main);
  178.  
  179. // load global config settings (load global variables into Util.java)
  180. loadConfig();
  181.  
  182. // initialise the message listeners
  183. initaliseReceivers();
  184.  
  185. // TODO uncomment for lockdown
  186. //initialise Lockdown
  187. //setupLockdown();
  188. //startLockdown();
  189.  
  190. // textview for displaying the database ID
  191. databaseTextView = (TextView) findViewById(R.id.database_textview);
  192. databaseTextView.bringToFront();
  193. databaseTextView.setText(Util.DATABASE_ID);
  194.  
  195. //hidden unlock button
  196. unlockButton = (Button) findViewById(R.id.button2);
  197. unlockButton.bringToFront();
  198. unlockButton.setOnClickListener(new View.OnClickListener() {
  199. public void onClick(View v) {
  200. // TODO uncomment for lockdown
  201. //stopLockdown();
  202. //startLockdown();
  203. }
  204. });
  205.  
  206. // setup webview
  207. mWebView = (WebView) findViewById(R.id.activity_main_webview);
  208.  
  209. // Force links and redirects to open in the WebView instead of in a browser
  210. mWebView.setWebChromeClient(new WebChromeClient());
  211. mWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);
  212. mWebView.getSettings().setLoadWithOverviewMode(true);
  213. mWebView.getSettings().setUseWideViewPort(true);
  214. mWebView.getSettings().setJavaScriptEnabled(true);
  215.  
  216. mWebView.getSettings().setAllowFileAccess(true);
  217. mWebView.getSettings().setAllowContentAccess(true);
  218.  
  219.  
  220. // mWebView.getSettings().setUserAgentString("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36");
  221.  
  222.  
  223.  
  224. // String url = new String("http://broken-links.com/tests/video/");
  225. //// mWebView.setWebChromeClient(new MyWebChromeClient(this));
  226. //// mWebView.setWebViewClient(new WebViewClient());
  227. // mWebView.setWebViewClient(n);
  228. // mWebView.getSettings().setJavaScriptEnabled(true);
  229. // mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
  230. // mWebView.loadUrl(url);
  231.  
  232. // mWebView = (WebView) findViewById(R.id.activity_main_webview);
  233. // mWebView.getSettings().setJavaScriptEnabled(true);
  234. // mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
  235. // mWebView.getSettings().setSupportMultipleWindows(true);
  236. // mWebView.getSettings().setSaveFormData(false);
  237. // mWebView.getSettings().setSavePassword(false);
  238. // mWebView.getSettings().setDomStorageEnabled(true);
  239. //
  240. // mWebView.getSettings().setAllowFileAccess(true);
  241. // mWebView.getSettings().setLightTouchEnabled(true);
  242. // mWebView.getSettings().setJavaScriptEnabled(true);
  243. // mWebView.getSettings().setBuiltInZoomControls(true);
  244. // mWebView.getSettings().setUseWideViewPort(true);
  245. // mWebView.getSettings().setLoadWithOverviewMode(true);
  246.  
  247.  
  248.  
  249. // getWindow().setFormat(PixelFormat.TRANSLUCENT);
  250. // VideoView videoHolder = new VideoView(this);
  251. // //if you want the controls to appear
  252. // videoHolder.setMediaController(new MediaController(this));
  253. //// Uri video = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.small); //do not add any extension
  254. //// videoHolder.setVideoURI(video);
  255. // videoHolder.setVideoPath("/data/user/0/px.ks1702/files/10001/screen5/content/videos/fbc0f56c611508725420.webm");
  256. // setContentView(videoHolder);
  257. // videoHolder.start();
  258.  
  259.  
  260.  
  261.  
  262.  
  263. // create token and register to database
  264.  
  265. // TODO change this so it only calls once. So it doesn't flood the server and network
  266. CreateToken();
  267.  
  268. sendIpAddress();
  269.  
  270. String indexFileLocation = "/data/user/0/px.ks1702/files/" + Util.CLIENT_ID + "/" + Util.SCREEN_ID + "/" + Util.INDEX_FILE_NAME;
  271. File index = new File(indexFileLocation);
  272.  
  273. Log.d(LOG_TAG, "Checking: " + indexFileLocation);
  274.  
  275. // check if there is already a local index1.hmtl file
  276. // if (index.exists()) {
  277. // // index exists
  278. // mWebView.loadUrl("file://" + index);
  279. // // so you can't see database id anymore
  280. // databaseTextView.setText("");
  281. // }
  282. // else {
  283. // // index doesn't exist
  284. // Log.d(LOG_TAG, "Can't find a local index file");
  285. // mWebView.loadUrl("file:///android_asset/www/loading.html");
  286. //
  287. // }
  288.  
  289. mWebView.loadUrl("file:///data/user/0/px.ks1702/files/10001/screen5/content/videos/fbc0f56c611508725420.webm");
  290.  
  291.  
  292. // String summary = "<html><body><video controls autoplay src='file:///data/user/0/px.ks1702/files/10001/screen5/content/videos/fbc0f56c611508725420.webm'></video></body></html>";
  293. // String summary = "<html><body><input type='file' accept='video/*'/> <video controls autoplay></video></body></html>";
  294. // mWebView.loadData(summary, "text/html", null);
  295. // mWebView.loadUrl("http://broken-links.com/tests/video/");
  296.  
  297. // mWebView.loadUrl("http://clips.vorwaerts-gmbh.de/big_buck_bunny.webm");
  298. // mWebView.loadUrl("file:///data/user/0/px.ks1702/files/10001/screen5/content/videos/fbc0f56c611508725420.webm");
  299. // mWebView.loadData("file:///data/user/0/px.ks1702/files/10001/screen5/content/videos/fbc0f56c611508725420.webm", "text/html", "utf-8");
  300. // mWebView.loadDataWithBaseURL(mStory.getUrl(), mStory.getUnparsedContent(),
  301. // "text/html; charset=utf-8", "UTF-8", null);
  302.  
  303.  
  304. Timer T = new Timer();
  305. T.schedule(new TimerTask()
  306. {
  307. @Override
  308. public void run()
  309. {
  310. DownloadContent(Util.CLIENT_ID + "/" + Util.SCREEN_ID);
  311. }
  312. }, (30 * 60000), (30 * 60000));
  313. }
  314.  
  315.  
  316. /*
  317. * Create Firebase Token and upload it to the database
  318. */
  319. public void CreateToken() {
  320. String token = FirebaseInstanceId.getInstance().getToken();
  321.  
  322. // Log and toast
  323. Log.d(LOG_TAG, "Creating token...");
  324.  
  325. Log.d(LOG_TAG, "InstanceID Token: " + token);
  326.  
  327. // regisister token in database
  328. MyFirebaseMessagingService service = new MyFirebaseMessagingService();
  329. // service.sendFtpSync("insert_ftp_sync_date_here", "insert_ftp_sync_update_date_here");
  330.  
  331. DeviceDetails device = new DeviceDetails();
  332. String mac = device.getMacAddress();
  333. Map<String, String> message = new HashMap<String, String>();
  334. message.put("token", token);
  335. message.put("mac", mac);
  336. message.put("command", "addToken");
  337.  
  338. service.sendMessage(message);
  339. }
  340. /*
  341. * Send the devices IP address to the database
  342. */
  343. public void sendIpAddress() {
  344. MyFirebaseMessagingService service = new MyFirebaseMessagingService();
  345. DeviceDetails device = new DeviceDetails();
  346. String mac = device.getMacAddress();
  347.  
  348. Map<String, String> message = new HashMap<String, String>();
  349. String ip = device.getIpAddress();
  350. message.put("ipAddress", ip);
  351. message.put("mac", mac);
  352. message.put("command", "addIpAddress");
  353.  
  354. service.sendMessage(message);
  355. }
  356.  
  357. /*
  358. * Determine if there is an internet connection
  359. */
  360. public void DetermineConnection() {
  361. Log.d(LOG_TAG, "Determining connection");
  362. // manually determine if connected to internet on start up
  363. NetworkStatus determineConnection = new NetworkStatus(this, new NetworkStatusInterface() {
  364. @Override
  365. public void hasActiveInternetConnection(boolean result) {
  366.  
  367. Log.d(LOG_TAG, "Result of internet connection: " + result);
  368.  
  369. if (result == true) {
  370. // connected successfully
  371. TextView textview = (TextView)findViewById(R.id.database_textview);
  372.  
  373. // Append -D to signal DOWNLOADING has begun
  374. if ( !textview.getText().equals("") ) {
  375. textview.setText(textview.getText() + "-D");
  376. }
  377.  
  378. DownloadContent(Util.CLIENT_ID + "/" + Util.SCREEN_ID);
  379. // DownloadContent("client1/screen1");
  380. } else {
  381. // connection failed
  382. mWebView.loadUrl("file:///android_asset/www/loading.html");
  383. databaseTextView.setText("Database ID: " + Util.DATABASE_ID);
  384. }
  385. }
  386. });
  387. // attempt to connect to the internet
  388. determineConnection.execute();
  389. }
  390.  
  391. /*
  392. * Download content for the FTP server
  393. */
  394. private void DownloadContent(String folderPath)
  395. {
  396.  
  397. // load config settings
  398. // PropertiesHandler config = new PropertiesHandler();
  399. //
  400. // String server = config.getConfigValue(this, "ftp_server");
  401. // String username = config.getConfigValue(this, "ftp_username");
  402. // String password = config.getConfigValue(this, "ftp_password");
  403.  
  404. String server = Util.FTP_SERVER;
  405. String username = Util.FTP_USERNAME;
  406. String password = Util.FTP_PASSWORD;
  407.  
  408. Log.d(LOG_TAG, server + " " + username + " " + password);
  409.  
  410. FtpDownloader test = new FtpDownloader(this, mWebView, databaseTextView);
  411.  
  412. test.execute(server, username, password, folderPath);
  413. }
  414.  
  415.  
  416. private void loadConfig() {
  417. // Restore preferences
  418. SharedPreferences settings = getPreferences(0);
  419.  
  420. // Get the property values
  421. // If it can't find the value. Set the string to the second parameter
  422. Util.FTP_SERVER = settings.getString("ftp_server", Util.FTP_SERVER);
  423. Util.FTP_USERNAME = settings.getString("ftp_username", Util.FTP_USERNAME);
  424. Util.FTP_PASSWORD = settings.getString("ftp_password", Util.FTP_PASSWORD);
  425.  
  426. Util.CLIENT_ID = settings.getString("client_id", Util.CLIENT_ID);
  427. Util.SCREEN_ID = settings.getString("screen_id", Util.SCREEN_ID);
  428. Util.DATABASE_ID = settings.getString("database_id", Util.DATABASE_ID);
  429.  
  430. Util.SENDER_ID = settings.getString("sender_id", Util.SENDER_ID);
  431. Util.INDEX_FILE_NAME = settings.getString("index_file_name", Util.INDEX_FILE_NAME);
  432.  
  433. Util.SCREEN_ORIENTATION = settings.getString("screen_orientation", Util.SCREEN_ORIENTATION);
  434.  
  435. setScreenOrientation();
  436.  
  437. Log.d(LOG_TAG, "Configuration loaded!");
  438. Util.printValues();
  439. }
  440.  
  441. // save global variables
  442. private void saveConfig() {
  443. SharedPreferences settings = getPreferences(0);
  444. SharedPreferences.Editor editor = settings.edit();
  445. editor.putString("ftp_server", Util.FTP_SERVER);
  446. editor.putString("ftp_username", Util.FTP_USERNAME);
  447. editor.putString("ftp_password", Util.FTP_PASSWORD);
  448.  
  449. editor.putString("client_id", Util.CLIENT_ID);
  450. editor.putString("screen_id", Util.SCREEN_ID);
  451. editor.putString("database_id", Util.DATABASE_ID);
  452.  
  453. editor.putString("sender_id", Util.SENDER_ID);
  454. editor.putString("index_file_name", Util.INDEX_FILE_NAME);
  455.  
  456. editor.putString("screen_orientation", Util.SCREEN_ORIENTATION);
  457.  
  458. // Commit the edits!
  459. editor.commit();
  460. Log.d(LOG_TAG, "Configuration saved!");
  461. }
  462.  
  463. private void initaliseReceivers() {
  464. // initialise the database ID update listener
  465. if (ftpSettingsReceiver != null) {
  466. IntentFilter intentFilter = new IntentFilter("updateDatabaseId");
  467. registerReceiver(ftpSettingsReceiver, intentFilter);
  468. }
  469. // initialise the database ID update listener
  470. if (screenOrientationReceiver != null) {
  471. IntentFilter intentFilter = new IntentFilter("updateScreenRotation");
  472. registerReceiver(screenOrientationReceiver, intentFilter);
  473. }
  474. // initialise the client and screen id update listener
  475. if (clientAndScreenIdReceiver != null) {
  476. IntentFilter intentFilter = new IntentFilter("updateClientAndScreenId");
  477. registerReceiver(clientAndScreenIdReceiver, intentFilter);
  478. }
  479.  
  480. if (reloadScreenReceiver != null) {
  481. IntentFilter intentFilter = new IntentFilter("reloadScreen");
  482. registerReceiver(reloadScreenReceiver, intentFilter);
  483. }
  484.  
  485. if (forceUpdateReceiver != null) {
  486. IntentFilter intentFilter = new IntentFilter("forceUpdate");
  487. registerReceiver(forceUpdateReceiver, intentFilter);
  488. }
  489.  
  490. if (lockdownReceiver != null) {
  491. IntentFilter intentFilter = new IntentFilter("handleLockdown");
  492. registerReceiver(lockdownReceiver, intentFilter);
  493. }
  494.  
  495. }
  496.  
  497. private void setScreenOrientation() {
  498. if (Util.SCREEN_ORIENTATION.equals("landscape")) {
  499. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  500. } else if (Util.SCREEN_ORIENTATION.equals("portrait")) {
  501. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  502. } else if (Util.SCREEN_ORIENTATION.equals("reverseLandscape")) {
  503. Log.d(LOG_TAG, "Reverse landscape recieved");
  504. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  505. } else if (Util.SCREEN_ORIENTATION.equals("reversePortrait")) {
  506. Log.d(LOG_TAG, "Reverse portrait recieved");
  507. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  508. }
  509. }
  510.  
  511. //So the contents of the webview does not disappear on rotation of device
  512. @Override
  513. protected void onSaveInstanceState(Bundle outState) {
  514. super.onSaveInstanceState(outState);
  515. mWebView.saveState(outState);
  516. }
  517.  
  518. //So the contents of the webview does not disappear on rotation of device
  519. @Override
  520. protected void onRestoreInstanceState(Bundle savedInstanceState) {
  521. super.onRestoreInstanceState(savedInstanceState);
  522. mWebView.restoreState(savedInstanceState);
  523. }
  524.  
  525. // TODO uncomment for lockdown
  526. /*
  527. void setupLockdown() {
  528. mDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
  529. mAdminComponentName = DeviceAdminReceiver.getComponentName(this);
  530. mPackageManager = this.getPackageManager();
  531. //stopLockdown();
  532. }
  533.  
  534. void stopLockdown() {
  535. ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
  536. if (am.getLockTaskModeState() == ActivityManager.LOCK_TASK_MODE_LOCKED) {
  537. stopLockTask();
  538. setDefaultCosuPolicies(false);
  539. }
  540. }
  541.  
  542. void startLockdown() {
  543. if(mDevicePolicyManager.isDeviceOwnerApp(getPackageName())){
  544. setDefaultCosuPolicies(true);
  545. }
  546. else {
  547. Log.d(LOG_TAG, "Not Device Owner. To set device owner factory reset then run adb shell dpm set-device-owner px.ks1702/.DeviceAdminReceiver ");
  548. }
  549. if(mDevicePolicyManager.isLockTaskPermitted(getPackageName())){
  550. ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
  551. if (am.getLockTaskModeState() == ActivityManager.LOCK_TASK_MODE_NONE) {
  552. startLockTask();
  553. }
  554. }
  555. }
  556.  
  557. private void setDefaultCosuPolicies(boolean active){
  558.  
  559. // Set user restrictions
  560. setUserRestriction(UserManager.DISALLOW_SAFE_BOOT, active);
  561. setUserRestriction(UserManager.DISALLOW_FACTORY_RESET, active);
  562. setUserRestriction(UserManager.DISALLOW_ADD_USER, active);
  563. setUserRestriction(UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA, active);
  564. setUserRestriction(UserManager.DISALLOW_ADJUST_VOLUME, active);
  565. //setUserRestriction(UserManager.DISALLOW_APPS_CONTROL, active);
  566. //setUserRestriction(UserManager.DISALLOW_NETWORK_RESET, active);
  567.  
  568. // Disable keyguard and status bar
  569. mDevicePolicyManager.setKeyguardDisabled(mAdminComponentName, active);
  570. mDevicePolicyManager.setStatusBarDisabled(mAdminComponentName, active);
  571.  
  572.  
  573. // Enable STAY_ON_WHILE_PLUGGED_IN
  574. enableStayOnWhilePluggedIn(active);
  575.  
  576. // Set system update policy
  577. if (active){
  578. mDevicePolicyManager.setSystemUpdatePolicy(mAdminComponentName,
  579. SystemUpdatePolicy.createWindowedInstallPolicy(60, 120));
  580. } else {
  581. mDevicePolicyManager.setSystemUpdatePolicy(mAdminComponentName,
  582. null);
  583. }
  584.  
  585. // set this Activity as a lock task package
  586. mDevicePolicyManager.setLockTaskPackages(mAdminComponentName,
  587. active ? new String[]{getPackageName()} : new String[]{});
  588.  
  589. IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MAIN);
  590. intentFilter.addCategory(Intent.CATEGORY_HOME);
  591. intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
  592.  
  593. if (active) {
  594. // set Cosu activity as home intent receiver so that it is started
  595. // on reboot
  596. mDevicePolicyManager.addPersistentPreferredActivity(
  597. mAdminComponentName, intentFilter, new ComponentName(
  598. getPackageName(), MainActivity.class.getName()));
  599. } else {
  600. mDevicePolicyManager.clearPackagePersistentPreferredActivities(
  601. mAdminComponentName, getPackageName());
  602. }
  603. }
  604.  
  605. private void setUserRestriction(String restriction, boolean disallow){
  606. if (disallow) {
  607. mDevicePolicyManager.addUserRestriction(mAdminComponentName, restriction);
  608. } else {
  609. mDevicePolicyManager.clearUserRestriction(mAdminComponentName, restriction);
  610. }
  611. }
  612.  
  613. private void enableStayOnWhilePluggedIn(boolean enabled){
  614. if (enabled) {
  615. mDevicePolicyManager.setGlobalSetting(
  616. mAdminComponentName, Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
  617. Integer.toString(BatteryManager.BATTERY_PLUGGED_AC
  618. | BatteryManager.BATTERY_PLUGGED_USB
  619. | BatteryManager.BATTERY_PLUGGED_WIRELESS));
  620. } else {
  621. mDevicePolicyManager.setGlobalSetting(mAdminComponentName,
  622. Settings.Global.STAY_ON_WHILE_PLUGGED_IN, "0");
  623. }
  624. }
  625. */
  626.  
  627. // Load config settings on device start up
  628. @Override
  629. protected void onStart() {
  630. super.onStart();
  631. Log.d(LOG_TAG, "onStart Called");
  632.  
  633. loadConfig();
  634. // TODO uncomment for lockdown
  635. //startLockdown();
  636. // PropertiesHandler prop = new PropertiesHandler();
  637. // prop.readProperities();
  638. // prop.loadProperities();
  639.  
  640. }
  641.  
  642. // Load config settings on device resume
  643. @Override
  644. protected void onResume() {
  645. super.onResume();
  646. Log.d(LOG_TAG, "onResume Called");
  647. loadConfig();
  648. // TODO uncomment for lockdown
  649. //startLockdown();
  650. }
  651.  
  652. // save config settings on device pause
  653. @Override
  654. protected void onPause() {
  655. super.onPause();
  656.  
  657. Log.d(LOG_TAG, "onPause Called");
  658. saveConfig();
  659. }
  660.  
  661. // save config settings on device stop
  662. @Override
  663. protected void onStop() {
  664. super.onStop(); // Always call the superclass method first
  665.  
  666. Log.d(LOG_TAG, "OnStop Called");
  667. //
  668. // PropertiesHandler prop = new PropertiesHandler();
  669. // prop.saveProperties();
  670.  
  671. saveConfig();
  672. }
  673. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement