PageRenderTime 73ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/droidcare-ui/src/main/java/com/googlecode/droidcare/ui/MainActivity.java

https://bitbucket.org/pbuitvydas/droid-care
Java | 188 lines | 146 code | 19 blank | 23 comment | 13 complexity | b33db23eefc143f36be414edd09d71e5 MD5 | raw file
  1. package com.googlecode.droidcare.ui;
  2. /*
  3. * #%L
  4. * droidcare-ui
  5. * %%
  6. * Copyright (C) 2012 - 2013 Paulius Buitvydas
  7. * %%
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. * #L%
  20. */
  21. import java.io.InputStream;
  22. import java.util.Map.Entry;
  23. import com.google.inject.Guice;
  24. import com.googlecode.droidcare.common.AppContext;
  25. import com.googlecode.droidcare.common.ScriptEngine;
  26. import com.googlecode.droidcare.common.di.GuiceModule;
  27. import com.googlecode.droidcare.common.provider.ProviderUpdateEvent;
  28. import com.googlecode.droidcare.common.provider.SelfServiceProvider;
  29. import com.googlecode.droidcare.common.provider.ProviderListener;
  30. import com.googlecode.droidcare.common.provider.ProviderManager;
  31. import com.googlecode.droidcare.common.usr.User;
  32. import com.googlecode.droidcare.common.usr.UserAccount;
  33. import android.net.ConnectivityManager;
  34. import android.net.NetworkInfo;
  35. import android.os.Bundle;
  36. import android.app.Activity;
  37. import android.app.ProgressDialog;
  38. import android.content.Context;
  39. import android.content.Intent;
  40. import android.text.Html;
  41. import android.view.Menu;
  42. import android.view.MenuItem;
  43. import android.view.View;
  44. import android.widget.TextView;
  45. import android.widget.Toast;
  46. public class MainActivity extends Activity implements ProviderListener {
  47. private TextView tvLikutis;
  48. private TextView tvSaskaitaGalioja;
  49. private TextView tvNumerisGalioja;
  50. private TextView tvHtml;
  51. static ProgressDialog dialog;
  52. @Override
  53. public void onCreate(Bundle savedInstanceState) {
  54. super.onCreate(savedInstanceState);
  55. Guice.createInjector(new GuiceModule());
  56. setContentView(R.layout.activity_main);
  57. tvLikutis = (TextView) findViewById(R.id.tvLikutis);
  58. tvHtml = (TextView) findViewById(R.id.tvHtml);
  59. //http://javatechig.com/android/display-html-in-android-textview/
  60. //tvHtml.setText(Html.fromHtml("<div align=\"center\">Labas</div>"), TextView.BufferType.SPANNABLE);
  61. tvSaskaitaGalioja = (TextView) findViewById(R.id.tvSaskaitaGaliojaIki);
  62. tvNumerisGalioja = (TextView) findViewById(R.id.tvNumerisGaliojaIki);
  63. dialog = new ProgressDialog(this);
  64. }
  65. @Override
  66. protected void onStart() {
  67. super.onStart();
  68. AppContext.getInstance().getProviderManager().loadBuiltInProviders();
  69. }
  70. public void updateUserLogin() {
  71. UserAccount userAccount = AppContext.getInstance().getUserAccount();
  72. userAccount.getUser().setLogin(Prefs.getLoginFromSettings(this));
  73. userAccount.getUser().setPassword(Prefs.getPassFromSettings(this));
  74. }
  75. @Override
  76. public boolean onCreateOptionsMenu(Menu menu) {
  77. getMenuInflater().inflate(R.menu.activity_main, menu);
  78. return true;
  79. }
  80. public void update(View v) {
  81. updateUserLogin();
  82. String selectedProviderId = Prefs.getProviderFromSettings(this);
  83. if (!validate(selectedProviderId)) {
  84. return;
  85. }
  86. showUpateDialog();
  87. SelfServiceProvider p = AppContext.getInstance().getProviderManager().get(selectedProviderId);
  88. p.updateAsync(this);
  89. }
  90. public void showUpateDialog() {
  91. dialog.setMessage("Atnaujinama...");
  92. dialog.setIndeterminate(true);
  93. dialog.setCancelable(false);
  94. dialog.show();
  95. }
  96. public void hideUpdatedialog() {
  97. dialog.hide();
  98. }
  99. public boolean isNetworkAvailable() {
  100. ConnectivityManager cm = (ConnectivityManager)
  101. getSystemService(Context.CONNECTIVITY_SERVICE);
  102. NetworkInfo networkInfo = cm.getActiveNetworkInfo();
  103. // if no network is available networkInfo will be null
  104. // otherwise check if we are connected
  105. if (networkInfo != null && networkInfo.isConnected()) {
  106. return true;
  107. }
  108. return false;
  109. }
  110. public boolean validate(String providerId) {
  111. UserAccount usrA = AppContext.getInstance().getUserAccount();
  112. boolean valid = true;
  113. if (isEmpty(usrA.getUser().getLogin())) {
  114. valid = false;
  115. Toast.makeText(MainActivity.this, "Nenurodytas prisijungimo vardas", Toast.LENGTH_SHORT).show();
  116. }
  117. if (isEmpty(usrA.getUser().getPassword())) {
  118. valid = false;
  119. Toast.makeText(MainActivity.this, "Nenurodytas prisijungimo slaptazodis", Toast.LENGTH_SHORT).show();
  120. }
  121. if (isEmpty(providerId)) {
  122. valid = false;
  123. Toast.makeText(MainActivity.this, "Nepasirinktas rysio tiekejas", Toast.LENGTH_SHORT).show();
  124. }
  125. if (!isNetworkAvailable()) {
  126. Toast.makeText(this, "Nera interneto rysio", Toast.LENGTH_SHORT).show();
  127. valid = false;
  128. }
  129. return valid;
  130. }
  131. private boolean isEmpty(String str) {
  132. if (str == null) {
  133. return true;
  134. }
  135. if (str.length() == 0) {
  136. return true;
  137. }
  138. return false;
  139. }
  140. @Override
  141. public boolean onOptionsItemSelected(MenuItem item) {
  142. switch (item.getItemId()) {
  143. case R.id.menu_settings:
  144. startActivity(new Intent(this, Prefs.class));
  145. return true;
  146. case R.id.menu_exit:
  147. finish();
  148. return true;
  149. }
  150. return false;
  151. }
  152. public void afterUpdate(SelfServiceProvider p) {
  153. hideUpdatedialog();
  154. Toast.makeText(MainActivity.this, "Atnaujinta sekmingai", Toast.LENGTH_SHORT).show();
  155. User usr = AppContext.getInstance().getUserAccount().getUser();
  156. tvLikutis.setText(usr.getLeft());
  157. tvSaskaitaGalioja.setText(usr.getValidTo());
  158. tvNumerisGalioja.setText(usr.getNumbValidTo());
  159. }
  160. @Override
  161. public void onUpdateComplete(final ProviderUpdateEvent e) {
  162. runOnUiThread(new Runnable() {
  163. @Override
  164. public void run() {
  165. afterUpdate(e.getProvider());
  166. }
  167. });
  168. }
  169. }