Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.act.weathertasks.activities;
- import java.util.ArrayList;
- import java.util.GregorianCalendar;
- import android.app.Activity;
- import android.app.Dialog;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.os.Handler;
- import android.preference.PreferenceManager;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ProgressBar;
- import android.widget.TextView;
- import com.act.weathertasks.R;
- import com.act.weathertasks.database.DatabaseAdapter;
- import com.act.weathertasks.objects.Task;
- import com.act.weathertasks.objects.TasksCore;
- import com.act.weathertasks.objects.Weather;
- import com.act.weathertasks.weather.WeatherGetter;
- public class StartupActivity extends Activity {
- private DatabaseAdapter databaseAdapter;
- private ArrayList<Task> tasks;
- private ProgressBar progressBar;
- private TextView progressLabel;
- public SharedPreferences prefs;
- private String username = null;
- private String password = null;
- public void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.startup);
- prefs = PreferenceManager.getDefaultSharedPreferences(this);
- progressLabel = (TextView) findViewById(R.id.startupLabel);
- progressBar = (ProgressBar) findViewById(R.id.startupBar);
- databaseAdapter = new DatabaseAdapter(this);
- DataParser dataParser = new DataParser();
- dataParser.execute();
- if (prefs.getString("USERNAME", null) == null || prefs.getString("PASSWORD", null) == null) {
- final Dialog dialog = new Dialog(StartupActivity.this);
- dialog.setContentView(R.layout.login);
- dialog.setTitle("Login");
- dialog.setCancelable(true);
- Button loginButton = (Button) dialog.findViewById(R.id.loginButton);
- Button cancelButton = (Button) dialog.findViewById(R.id.cancelButton);
- final EditText usernameText = (EditText) dialog.findViewById(R.id.usernameText);
- final EditText passwordText = (EditText) dialog.findViewById(R.id.passwordText);
- loginButton.setOnClickListener(new OnClickListener() {
- public void onClick(View t) {
- username = usernameText.getText().toString();
- password = passwordText.getText().toString();
- dialog.dismiss();
- }
- });
- cancelButton.setOnClickListener(new OnClickListener() {
- public void onClick(View t) {
- dialog.dismiss();
- }
- });
- dialog.show();
- SharedPreferences.Editor editor = prefs.edit();
- editor.putString("USERNAME", username);
- editor.putString("PASSWORD", password);
- editor.commit();
- } else {
- username = prefs.getString("USERNAME", null);
- password = prefs.getString("PASSWORD", null);
- }
- /*
- try {
- login()
- } catch (Failed login) {
- // give login screen again with toast of incorrect data
- }
- */
- }
- public final class DataParser extends AsyncTask<Object, String, String> {
- @Override
- protected String doInBackground(Object... params) {
- publishProgress("Initializing: 0%");
- GregorianCalendar date = new GregorianCalendar();
- Task task1 = new Task("Task1", "location:sdk#weather:rain#temp:25#wind:0", date, date);
- Task task2 = new Task("Task2", "location:sdk#weather:sunny#temp:-5#wind:30", date,date);
- Task task3 = new Task("Task3", "location:asf#weather:rain#temp:20", date,date);
- Task task4 = new Task("Task4", "location:asf#weather:fog", date,date);
- Task task5 = new Task("Task5", "location:helsinki#weather:cloudy#temp:6", date,date);
- Task task6 = new Task("Task6", "location:helsinki", date,date);
- Task task7 = new Task("Task7", "location:almelo#weather:clear", date,date);
- Task task8 = new Task("Task8", "location:almelo#weather:cloudy#wind:14", date,date);
- Task task9 = new Task("Task9", "location:amsterdam#weather:fog", date,date);
- Task task10 = new Task("Task10", "location:amsterdam#weather:rain#temp:30#wind:40", date,date);
- publishProgress("Loaded tasks: 33%");
- progressBar.setProgress(33);
- databaseAdapter.insertTask(task1);
- databaseAdapter.insertTask(task2);
- databaseAdapter.insertTask(task3);
- databaseAdapter.insertTask(task4);
- databaseAdapter.insertTask(task5);
- databaseAdapter.insertTask(task6);
- databaseAdapter.insertTask(task7);
- databaseAdapter.insertTask(task8);
- databaseAdapter.insertTask(task9);
- databaseAdapter.insertTask(task10);
- publishProgress("Inserted tasks: 67%");
- progressBar.setProgress(67);
- tasks = databaseAdapter.getAllTasks();
- int progress = 67;
- for (Task t : tasks) {
- if (hasInternet()) {
- t.setWeather(WeatherGetter.getWeather(t.getDescriptionObject().getLocation()));
- } else {
- t.setWeather(new Weather("?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?"));
- }
- databaseAdapter.insertWeatherForTask(t);
- progress = progress + (33/tasks.size());
- publishProgress("Getting weather: " + progress + "%");
- progressBar.setProgress(progress);
- }
- publishProgress("Done: 100%");
- progressBar.setProgress(100);
- TasksCore.fillTasks(tasks);
- return "DONE";
- }
- @Override
- protected void onProgressUpdate(String... params) {
- progressLabel.setText(params[0]);
- }
- @Override
- protected void onPostExecute(String params) {
- new Handler().post(finishMain);
- }
- }
- private Runnable finishMain = new Runnable() {
- public void run() {
- Intent i = new Intent(getApplicationContext(), TabLayoutActivity.class);
- startActivity(i);
- finish();
- }
- };
- public boolean hasInternet() {
- ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo netInfo = cm.getActiveNetworkInfo();
- if (netInfo != null && netInfo.isConnectedOrConnecting()) {
- return true;
- }
- return false;
- }
- }
Add Comment
Please, Sign In to add comment