Guest User

Untitled

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