Guest User

Untitled

a guest
Feb 12th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. package com.act.weathertasks.activities;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.GregorianCalendar;
  5.  
  6. import android.app.Activity;
  7. import android.app.Dialog;
  8. import android.content.Context;
  9. import android.content.Intent;
  10. import android.content.SharedPreferences;
  11. import android.net.ConnectivityManager;
  12. import android.net.NetworkInfo;
  13. import android.os.AsyncTask;
  14. import android.os.Bundle;
  15. import android.os.Handler;
  16. import android.preference.PreferenceManager;
  17. import android.view.View;
  18. import android.view.View.OnClickListener;
  19. import android.widget.Button;
  20. import android.widget.EditText;
  21. import android.widget.ProgressBar;
  22. import android.widget.TextView;
  23.  
  24. import com.act.weathertasks.R;
  25. import com.act.weathertasks.database.DatabaseAdapter;
  26. import com.act.weathertasks.objects.Task;
  27. import com.act.weathertasks.objects.TasksCore;
  28. import com.act.weathertasks.objects.Weather;
  29. import com.act.weathertasks.weather.WeatherGetter;
  30.  
  31. public class StartupActivity extends Activity {
  32. private DatabaseAdapter databaseAdapter;
  33. private ArrayList<Task> tasks;
  34. private ProgressBar progressBar;
  35. private TextView progressLabel;
  36.  
  37. public SharedPreferences prefs;
  38. private String username = null;
  39. private String password = null;
  40.  
  41. public void onCreate(Bundle savedInstanceState){
  42. super.onCreate(savedInstanceState);
  43. setContentView(R.layout.startup);
  44.  
  45. prefs = PreferenceManager.getDefaultSharedPreferences(this);
  46.  
  47. progressLabel = (TextView) findViewById(R.id.startupLabel);
  48. progressBar = (ProgressBar) findViewById(R.id.startupBar);
  49.  
  50. databaseAdapter = new DatabaseAdapter(this);
  51.  
  52. DataParser dataParser = new DataParser();
  53. dataParser.execute();
  54.  
  55. if (prefs.getString("USERNAME", null) == null || prefs.getString("PASSWORD", null) == null) {
  56. final Dialog dialog = new Dialog(StartupActivity.this);
  57. dialog.setContentView(R.layout.login);
  58. dialog.setTitle("Login");
  59. dialog.setCancelable(true);
  60. Button loginButton = (Button) dialog.findViewById(R.id.loginButton);
  61. Button cancelButton = (Button) dialog.findViewById(R.id.cancelButton);
  62. final EditText usernameText = (EditText) dialog.findViewById(R.id.usernameText);
  63. final EditText passwordText = (EditText) dialog.findViewById(R.id.passwordText);
  64. loginButton.setOnClickListener(new OnClickListener() {
  65. public void onClick(View t) {
  66. username = usernameText.getText().toString();
  67. password = passwordText.getText().toString();
  68. dialog.dismiss();
  69. }
  70. });
  71. cancelButton.setOnClickListener(new OnClickListener() {
  72. public void onClick(View t) {
  73. dialog.dismiss();
  74. }
  75. });
  76. dialog.show();
  77.  
  78.  
  79. SharedPreferences.Editor editor = prefs.edit();
  80. editor.putString("USERNAME", username);
  81. editor.putString("PASSWORD", password);
  82. editor.commit();
  83. } else {
  84. username = prefs.getString("USERNAME", null);
  85. password = prefs.getString("PASSWORD", null);
  86. }
  87.  
  88. /*
  89. try {
  90. login()
  91. } catch (Failed login) {
  92. // give login screen again with toast of incorrect data
  93. }
  94. */
  95. }
  96.  
  97. public final class DataParser extends AsyncTask<Object, String, String> {
  98.  
  99. @Override
  100. protected String doInBackground(Object... params) {
  101. publishProgress("Initializing: 0%");
  102.  
  103. GregorianCalendar date = new GregorianCalendar();
  104.  
  105. Task task1 = new Task("Task1", "location:sdk#weather:rain#temp:25#wind:0", date, date);
  106. Task task2 = new Task("Task2", "location:sdk#weather:sunny#temp:-5#wind:30", date,date);
  107. Task task3 = new Task("Task3", "location:asf#weather:rain#temp:20", date,date);
  108. Task task4 = new Task("Task4", "location:asf#weather:fog", date,date);
  109. Task task5 = new Task("Task5", "location:helsinki#weather:cloudy#temp:6", date,date);
  110. Task task6 = new Task("Task6", "location:helsinki", date,date);
  111. Task task7 = new Task("Task7", "location:almelo#weather:clear", date,date);
  112. Task task8 = new Task("Task8", "location:almelo#weather:cloudy#wind:14", date,date);
  113. Task task9 = new Task("Task9", "location:amsterdam#weather:fog", date,date);
  114. Task task10 = new Task("Task10", "location:amsterdam#weather:rain#temp:30#wind:40", date,date);
  115.  
  116. publishProgress("Loaded tasks: 33%");
  117. progressBar.setProgress(33);
  118.  
  119. databaseAdapter.insertTask(task1);
  120. databaseAdapter.insertTask(task2);
  121. databaseAdapter.insertTask(task3);
  122. databaseAdapter.insertTask(task4);
  123. databaseAdapter.insertTask(task5);
  124. databaseAdapter.insertTask(task6);
  125. databaseAdapter.insertTask(task7);
  126. databaseAdapter.insertTask(task8);
  127. databaseAdapter.insertTask(task9);
  128. databaseAdapter.insertTask(task10);
  129.  
  130. publishProgress("Inserted tasks: 67%");
  131. progressBar.setProgress(67);
  132.  
  133. tasks = databaseAdapter.getAllTasks();
  134.  
  135. int progress = 67;
  136. for (Task t : tasks) {
  137. if (hasInternet()) {
  138. t.setWeather(WeatherGetter.getWeather(t.getDescriptionObject().getLocation()));
  139. } else {
  140. t.setWeather(new Weather("?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?"));
  141. }
  142. databaseAdapter.insertWeatherForTask(t);
  143. progress = progress + (33/tasks.size());
  144. publishProgress("Getting weather: " + progress + "%");
  145. progressBar.setProgress(progress);
  146. }
  147.  
  148. publishProgress("Done: 100%");
  149. progressBar.setProgress(100);
  150.  
  151. TasksCore.fillTasks(tasks);
  152. return "DONE";
  153. }
  154.  
  155. @Override
  156. protected void onProgressUpdate(String... params) {
  157. progressLabel.setText(params[0]);
  158. }
  159.  
  160. @Override
  161. protected void onPostExecute(String params) {
  162. new Handler().post(finishMain);
  163. }
  164.  
  165. }
  166.  
  167. private Runnable finishMain = new Runnable() {
  168. public void run() {
  169. Intent i = new Intent(getApplicationContext(), TabLayoutActivity.class);
  170. startActivity(i);
  171. finish();
  172. }
  173. };
  174.  
  175. public boolean hasInternet() {
  176. ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  177. NetworkInfo netInfo = cm.getActiveNetworkInfo();
  178. if (netInfo != null && netInfo.isConnectedOrConnecting()) {
  179. return true;
  180. }
  181. return false;
  182. }
  183. }
Add Comment
Please, Sign In to add comment