Advertisement
23222

Home

Mar 28th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.62 KB | None | 0 0
  1. package com.example.nousappsystems.websitenotification;
  2. import android.accessibilityservice.AccessibilityService;
  3. import android.app.AlarmManager;
  4. import android.app.Notification;
  5. import android.content.SharedPreferences;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.app.IntentService;
  9. import android.app.NotificationManager;
  10. import android.app.PendingIntent;
  11. import android.app.Service;
  12. import android.content.Context;
  13. import android.content.Intent;
  14. import android.os.HandlerThread;
  15. import android.os.IBinder;
  16. import android.os.Looper;
  17. import android.os.Message;
  18. import android.support.v7.app.AppCompatActivity;
  19. import android.os.Bundle;
  20. import android.app.Activity;
  21. import android.os.Bundle;
  22. import android.os.Handler;
  23. import android.support.v7.app.NotificationCompat;
  24. import android.util.Log;
  25. import android.view.View;
  26. import android.webkit.*;
  27. import android.widget.Button;
  28. import android.widget.ImageView;
  29. import android.widget.ProgressBar;
  30. import android.widget.TextView;
  31. import android.widget.Toast;
  32.  
  33. import java.text.SimpleDateFormat;
  34. import java.util.Calendar;
  35. import java.util.Date;
  36. import java.util.Random;
  37. import java.util.Timer;
  38. import java.util.TimerTask;
  39. /**
  40. * Created by NousappSystems on 3/25/2017.
  41. */
  42.  
  43. public class Home extends AppCompatActivity {
  44. private static final String TAG = Login.class.getSimpleName();
  45.  
  46.  
  47.  
  48. // public TextintView text2;
  49. private Button Button2;
  50. private Button Settings;
  51. private Button mJavaButton;
  52. private WebView mWebView;
  53. private WebView mWebView2;
  54. private Handler mHandler;
  55. Handler handler;
  56. String text = "hello";
  57. Context pref;
  58. String User ="test";
  59. String Pword = "hell";
  60. String txt1 = null;
  61. String test = null;
  62. String ctest = "NORMAL";
  63. String dtest = "Critical Level Reached";
  64. String etest = "Variation Level Reached";
  65. Timer timer;
  66. TimerTask timerTask;
  67. int notifyId;
  68. public static final String MyPREFERENCES = "MyPrefs" ;
  69. public static final String Name = "nameKey";
  70. public static final String Password = "Password";
  71.  
  72. SharedPreferences sharedpreferences;
  73. final Context context = this;
  74.  
  75. protected void onCreate(Bundle savedInstanceState) {
  76. super.onCreate(savedInstanceState);
  77. setContentView(R.layout.activity_main);
  78.  
  79.  
  80. Toast.makeText(getBaseContext(), "WELCOME TO TEMPMITR", Toast.LENGTH_LONG).show();
  81. mHandler = new Handler();
  82.  
  83. mJavaButton = (Button) findViewById(R.id.bt_javaButton);
  84. Button2 = (Button) findViewById(R.id.button2);
  85. Settings = (Button) findViewById(R.id.settings);
  86. mWebView = (WebView) findViewById(R.id.wv_main);
  87. mWebView2 = (WebView) findViewById(R.id.wv_main2);
  88.  
  89.  
  90.  
  91.  
  92. // Enabling JS
  93. mWebView.getSettings().setJavaScriptEnabled(true);
  94. mWebView2.getSettings().setJavaScriptEnabled(true);
  95.  
  96. // Java and Javascript interfacing
  97. mWebView.addJavascriptInterface(new Home.JavascriptInterface(), "JsInterface");
  98. mWebView2.addJavascriptInterface(new Home.JavascriptInterface(), "JsInterface");
  99.  
  100. // for debugging, this will handle the console.log() in javascript
  101. String n="adminnvh";
  102. String ph="adminnvh123";
  103.  
  104.  
  105. mWebView.loadUrl("javascript:changeBackgroundColor()");
  106. mWebView.loadUrl("http://www.nousappsystems.com/framework/index.php/TempMiter?user="+n+"&pword="+ph+"");
  107. mWebView.setWebChromeClient(new WebChromeClient() {
  108. @Override
  109. public boolean onConsoleMessage(ConsoleMessage cm) {
  110. Log.d(TAG, cm.message() + " ##########" + cm.lineNumber() + " -%%%%%%%%%-" + cm.sourceId());
  111. // refresh();
  112.  
  113. // mWebView.loadUrl("http://www.nousappsystems.com/gprs/TempMitr/TempMitrView.php");
  114. test = cm.message();
  115. txt1 = test.toString();
  116. //
  117. if (test == text) {
  118. // Toast.makeText(getBaseContext(), "WELCOME TO TEMPMITR" + test, Toast.LENGTH_LONG).show();
  119.  
  120. } else {
  121. // Toast.makeText(getBaseContext(), "The String is " + test, Toast.LENGTH_LONG).show();
  122. //
  123.  
  124. }
  125. return true;
  126. }
  127. });
  128. mJavaButton.setOnClickListener(new View.OnClickListener() {
  129. @Override
  130. public void onClick(View v) {
  131.  
  132. if(notifyId == 0){
  133. notifyId =0;
  134. Toast.makeText(Home.this,"Notification Disabled",Toast.LENGTH_LONG).show();
  135. ImageView img=(ImageView)findViewById(R.id.move_button);
  136. img.setVisibility(View.INVISIBLE);
  137.  
  138.  
  139.  
  140. }else{
  141.  
  142. notifyId =1;
  143. Toast.makeText(Home.this,"Notification Enabled",Toast.LENGTH_LONG).show();
  144. ImageView img=(ImageView)findViewById(R.id.move_button);
  145. img.setVisibility(View.VISIBLE);
  146. }
  147.  
  148. }
  149. });
  150.  
  151.  
  152.  
  153.  
  154. Button2.setOnClickListener(new View.OnClickListener() {
  155. @Override
  156. public void onClick(View v) {
  157.  
  158. Intent myIntents = new Intent (context,MainActivity.class);
  159. startActivity(myIntents);
  160.  
  161. }
  162. });
  163. Settings.setOnClickListener(new View.OnClickListener() {
  164. @Override
  165. public void onClick(View v) {
  166.  
  167. Intent myIntents = new Intent (context,GeneralSetting.class);
  168. startActivity(myIntents);
  169.  
  170. }
  171. });
  172. // mWebView.loadUrl("http://www.nousappsystems.com/gprs/TempMitr/TempMitrView.php");
  173. // doFakeWork();
  174.  
  175.  
  176. // LoginCredentials();
  177.  
  178.  
  179. }
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193. private void addNotification(String txt1) {
  194. //wv1.loadUrl("www.google.com");
  195.  
  196.  
  197. NotificationCompat.Builder builder =
  198. (NotificationCompat.Builder) new NotificationCompat.Builder(this)
  199. .setSmallIcon(R.drawable.notification)
  200. .setContentTitle(txt1)
  201. .setContentText("Temperature is Exceeded");
  202.  
  203. Log.i(TAG, "onClickJavascriptButton entered" + txt1 + ctest);
  204.  
  205.  
  206. if (txt1.equals("NORMAL")) { //txt1.equals("")
  207. Log.i(TAG, "onClickJavascriptButton entered" + txt1 + ctest);
  208. } else {
  209.  
  210. Log.i(TAG, "onClickJavascriptButton Else entered" + txt1 + ctest);
  211. builder.setDefaults(Notification.DEFAULT_SOUND);
  212. }
  213.  
  214.  
  215. Intent notificationIntent = new Intent(this, Login.class);
  216. PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
  217. PendingIntent.FLAG_UPDATE_CURRENT);
  218. builder.setContentIntent(contentIntent);
  219.  
  220. // Add as notification
  221. NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  222. manager.notify(0, builder.build());
  223. }
  224.  
  225. public void onClickJavaButton(View mView) {
  226. mWebView.loadUrl("javascript:changeBackgroundColor()");
  227. }
  228.  
  229. class JavascriptInterface {
  230.  
  231. JavascriptInterface() {
  232. }
  233.  
  234. public void onClickJavascriptButton() {
  235. Log.d(TAG, "onClickJavascriptButton entered");
  236. mHandler.post(new Runnable() {
  237. public void run() {
  238. Log.d(TAG, "onClickJavascriptButton inside run");
  239. if (mJavaButton.getText().toString().compareTo(getResources().getString(R.string.buttonText)) == 0)
  240. mJavaButton.setText(R.string.buttonText);
  241. else
  242. mJavaButton.setText(R.string.buttonText);
  243. }
  244. });
  245. }
  246. }
  247.  
  248.  
  249. @Override
  250. protected void onResume() {
  251. super.onResume();
  252.  
  253. //onResume we start our timer so it can start when the app comes from the background
  254. // String n= sharedpreferences.getString("nameKey", null);
  255. // String ph= sharedpreferences.getString("Password", null);
  256.  
  257. // if(n != null || ph != null){
  258. // startTimer();//ESSENCE OF THE APP
  259.  
  260.  
  261. // }
  262.  
  263. startTimer();
  264.  
  265. }
  266.  
  267. public void startTimer() {
  268. //set a new Timer
  269. timer = new Timer();
  270.  
  271. //initialize the TimerTask's job
  272. initializeTimerTask();
  273.  
  274. //schedule the timer, after the first 5000ms the TimerTask will run every 10000ms
  275. timer.schedule(timerTask, 5000, 10000); //
  276. }
  277.  
  278. public void stoptimertask(View v) {
  279. //stop the timer, if it's not already null
  280. if (timer != null) {
  281. timer.cancel();
  282. timer = null;
  283. }
  284. }
  285.  
  286. public void initializeTimerTask() {
  287.  
  288. timerTask = new TimerTask() {
  289. public void run() {
  290.  
  291. //use a handler to run a toast that shows the current timestamp
  292. // handler=new Handler();
  293. mHandler.post(new Runnable() {
  294. public void run() {
  295.  
  296. String n= sharedpreferences.getString("nameKey", Name);
  297. String ph= sharedpreferences.getString("Password", Password);
  298. // String n="test";
  299. // String ph="test";
  300. mWebView.loadUrl("javascript:changeBackgroundColor()");
  301. mWebView.loadUrl("http://www.nousappsystems.com/framework/index.php/TempMiter?user="+n+"&pword="+ph+"");
  302. mWebView.setWebChromeClient(new WebChromeClient() {
  303. @Override
  304. public boolean onConsoleMessage(ConsoleMessage cm) {
  305. Log.d(TAG, cm.message() + " ##########" + cm.lineNumber() + " -%%%%%%%%%-" + cm.sourceId());
  306. // refresh();
  307.  
  308. // mWebView.loadUrl("http://www.nousappsystems.com/gprs/TempMitr/TempMitrView.php");
  309. test = cm.message();
  310. txt1 = test.toString();
  311.  
  312. if ((test.equals(ctest))) {
  313.  
  314.  
  315. } else if ((test.startsWith("Uncaught"))) {
  316.  
  317.  
  318. }else if(notifyId != 1) {
  319.  
  320.  
  321. }
  322.  
  323. else if ((test.startsWith("SVG's SMIL"))) {
  324.  
  325.  
  326. }
  327.  
  328.  
  329.  
  330. else {
  331.  
  332. addNotification(txt1);
  333.  
  334. }
  335.  
  336.  
  337. return true;
  338. }
  339. });
  340. }
  341. });
  342. }
  343. };
  344. }
  345.  
  346.  
  347. //thread second
  348.  
  349.  
  350. public void startProgress(View view) {
  351. // do something long
  352. Runnable runnable = new Runnable() {
  353. @Override
  354. public void run() {
  355. for (int i = 0; i <= 10; i++) {
  356. final int value = i;
  357. doFakeWork();
  358.  
  359. }
  360. }
  361. };
  362. new Thread(runnable).start();
  363. }
  364.  
  365. // Simulating something timeconsuming
  366. private void doFakeWork() {
  367. try {
  368. Thread.sleep(1); //for 1 sec 1000 need to enter
  369. Toast.makeText(getBaseContext(), "fake work ", Toast.LENGTH_LONG).show();
  370. } catch (InterruptedException e) {
  371. e.printStackTrace();
  372. }
  373. }
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement