tlmarker1968

Untitled

Feb 5th, 2020
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.12 KB | None | 0 0
  1. package com.phoenixhospman.launcher1;
  2.  
  3. import android.annotation.SuppressLint;
  4. import android.app.Activity;
  5. import android.content.ContentValues;
  6. import android.content.Intent;
  7. import android.database.Cursor;
  8. import android.net.Uri;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. import android.view.WindowManager;
  12. import android.widget.Button;
  13. import android.widget.EditText;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16. import androidx.annotation.NonNull;
  17. import androidx.appcompat.app.AlertDialog;
  18. import java.util.List;
  19. import retrofit2.Call;
  20. import retrofit2.Callback;
  21. import retrofit2.Response;
  22. import static android.view.View.inflate;
  23. import static java.util.Objects.requireNonNull;
  24.  
  25. /**
  26.  * The main activity for the app.
  27.  * @author Troy L. Marker
  28.  * @version 1.0.0
  29.  * @since 0.1.0
  30.  */
  31. public class ActivityHome extends Activity implements View.OnClickListener {
  32.     private ManagerSecurityApi managerSecurityApi;
  33.     final private static int REQUEST_CODE_1 = 1;
  34.     private String strCoName;
  35.     private String strApiUrl;
  36.     private EditText etUsername;
  37.     private EditText etPassword;
  38.     private ObjectUser objectUser;
  39.  
  40.     /**
  41.      * This method will create the activity, read content to display, and show the main activity screen
  42.      * Used when the activity is (re)created.
  43.      *
  44.      * @param savedInstanceState current saved instance state
  45.      */
  46.     @Override
  47.     protected void onCreate(Bundle savedInstanceState) {
  48.         super.onCreate(savedInstanceState);
  49.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  50.         View decorView = getWindow().getDecorView();
  51.         decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
  52.         setContentView(R.layout.activity_home);
  53.         etUsername = findViewById(R.id.etUsername);
  54.         etPassword = findViewById(R.id.etPassword);
  55.         TextView tvWarning = findViewById(R.id.tvWarning);
  56.         Button btnLogon = findViewById(R.id.btnLogon);
  57.         btnLogon.setOnClickListener(this);
  58.         @SuppressLint ("Recycle") Cursor cursor = getContentResolver().query(Uri.parse("content://com.phoenixhospman.installer.ProviderSettings/settings"), null, null, null, null, null);
  59.         assert cursor != null;
  60.         if(cursor.moveToFirst()) {
  61.             while(!cursor.isAfterLast()) {
  62.                 strCoName = cursor.getString(cursor.getColumnIndex("coname"));
  63.                 strApiUrl = cursor.getString(cursor.getColumnIndex("apiurl"));
  64.                 cursor.moveToNext();
  65.             }
  66.             managerSecurityApi = new ManagerSecurityApi(strApiUrl);
  67.             tvWarning.setText(getString(R.string.warning, rtrim(strCoName)));
  68.         } else {
  69.             Error("\nRequired setting missing.\nPlease (re)run Phoenix Install App");
  70.         }
  71.  
  72.     }
  73.  
  74.     /**
  75.      *
  76.      * This method will read check the entered credentials for proper access
  77.      *
  78.      * @param view the view calling the method.
  79.      */
  80.     public void onClick(final View view) {
  81.         if (view.getId() == R.id.btnLogon) {
  82.             managerSecurityApi.login (etUsername.getText ().toString (), etPassword.getText ().toString (), new Callback<List<ObjectUser>> () {
  83.                 @Override
  84.                 public void onResponse (@NonNull Call<List<ObjectUser>> call, @NonNull Response<List<ObjectUser>> response) {
  85.                     List<ObjectUser> JsonList = requireNonNull(response).body ();
  86.                     for (int i = 0; i < requireNonNull (JsonList).size (); i++)
  87.                         objectUser = new ObjectUser (
  88.                             JsonList.get (i).getId (),
  89.                             JsonList.get (i).getUsername (),
  90.                             JsonList.get (i).getGrade (),
  91.                             JsonList.get (i).getGradename(),
  92.                             JsonList.get (i).getDepartment (),
  93.                             JsonList.get (i).getDepartmentname()
  94.                         );
  95.                     getContentResolver ().delete (ProviderUser.CONTENT_URI, null, null);
  96.                     etUsername.setText (R.string.blank);
  97.                     etPassword.setText (R.string.blank);
  98.                     ContentValues values = new ContentValues ();
  99.                     values.put (ProviderUser.name, objectUser.getUsername ());
  100.                     values.put (ProviderUser.grade, String.valueOf (objectUser.getGrade ()));
  101.                     values.put (ProviderUser.gradename, objectUser.getGradename ());
  102.                     values.put (ProviderUser.department, String.valueOf (objectUser.getDepartment ()));
  103.                     values.put (ProviderUser.departmentname, objectUser.getDepartmentname());
  104.                     getContentResolver ().insert (ProviderUser.CONTENT_URI, values);
  105.                     showApps ();
  106.                 }
  107.                 @Override
  108.                 public void onFailure (@NonNull Call<List<ObjectUser>> call, @NonNull Throwable t) {
  109.                     Toast.makeText (getApplicationContext (), t.getMessage (), Toast.LENGTH_SHORT).show ();
  110.                 }
  111.             });
  112.         } else {
  113.             throw new IllegalStateException("Unexpected value: " + view.getId());
  114.         }
  115.     }
  116.  
  117.     /**
  118.      * This method will call the App Display Activity
  119.      */
  120.     private void showApps(){
  121.         Intent intent = new Intent(ActivityHome.this.getApplicationContext(), ActivityApp.class);
  122.         intent.putExtra("username", objectUser.getUsername());
  123.         intent.putExtra("grade", String.valueOf(objectUser.getGrade()));
  124.         intent.putExtra("department", String.valueOf(objectUser.getDepartment()));
  125.         startActivityForResult(intent, REQUEST_CODE_1);
  126.     }
  127.  
  128.     /**
  129.      * Override of the onBackPress method from the parent class
  130.      * This disable th back button on the device.
  131.      *
  132.      */
  133.     @Override
  134.     public void onBackPressed() {
  135.     }
  136.  
  137.     /**
  138.      * The error display method
  139.      *
  140.      * This method displays a dialog box with an error message and a close button.
  141.      *
  142.      * @param strError the error message to display
  143.      */
  144.     @SuppressWarnings ("SameParameterValue")
  145.     private void Error(String strError) {
  146.         AlertDialog.Builder mBuilder = new AlertDialog.Builder(this);
  147.         View view = inflate(this, R.layout.dialog_error, null);
  148.         Button btnExit = view.findViewById(R.id.buttonExitButton);
  149.         Button btnError = view.findViewById(R.id.btnErrorMessage);
  150.         btnError.setText(getString(R.string.error, strError ));
  151.         mBuilder.setView(view);
  152.         AlertDialog dialog = mBuilder.create();
  153.         dialog.setCanceledOnTouchOutside(false);
  154.         dialog.setCancelable(false);
  155.         dialog.show();
  156.         btnExit.setOnClickListener(v -> finishAffinity());
  157.     }
  158.  
  159.     /**
  160.      * Function to trim whitespace from the end of a string.
  161.      *
  162.      * @param s the string to trim
  163.      * @return the trimmed string
  164.      */
  165.     public static String rtrim(String s) {
  166.         int i = s.length()-1;
  167.         while (i >= 0 && Character.isWhitespace(s.charAt(i))) {
  168.             i--;
  169.         }
  170.         return s.substring(0,i+1);
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment