Android Open Source - sana Moca






From Project

Back to project page sana.

License

The source code is released under:

Copyright (c) 2010, Moca All rights reserved. The source code for Moca is licensed under the BSD license as follows: Redistribution and use in source and binary forms, with or without modification, ...

If you think the Android project sana listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

??package org.moca.activity;
import android.graphics.Typeface;
import android.widget.Button;
import android.widget.Toast;
//w w  w .java  2 s .  c  o  m
import org.moca.Constants;
import org.moca.R;
import org.moca.db.MocaDB.NotificationSQLFormat;
import org.moca.db.MocaDB.ProcedureSQLFormat;
import org.moca.db.MocaDB.SavedProcedureSQLFormat;
import org.moca.service.BackgroundUploader;
import org.moca.service.ServiceConnector;
import org.moca.service.ServiceListener;
import org.moca.task.CheckCredentialsTask;
import org.moca.task.MDSSyncTask;
import org.moca.task.ResetDatabaseTask;
import org.moca.task.ValidationListener;
import org.moca.util.MocaUtil;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.graphics.Typeface;
/**
 * Main Moca activity. When Moca is launched, this activity runs, allowing the user to 
 * either run a procedure, view notifications, or view pending transfers.
 */
public class Moca extends Activity implements View.OnClickListener {
    public static final String TAG = Moca.class.toString();

    // Option menu codes
    private static final int OPTION_RELOAD_DATABASE = 0;
    private static final int OPTION_SETTINGS = 1;
  private static final int OPTION_SYNC = 2;
  private static final int OPTION_LANGUAGE = 3;
    // Activity request codes
    public static final int PICK_PROCEDURE = 0;
    public static final int PICK_SAVEDPROCEDURE = 1;
    public static final int PICK_NOTIFICATION = 2;
    public static final int RUN_PROCEDURE = 3;
    public static final int RESUME_PROCEDURE = 4;
    public static final int SETTINGS = 6;
    
    //Alert dialog codes
    private static final int DIALOG_INCORRECT_PASSWORD = 0;
  private static final int DIALOG_NO_CONNECTIVITY = 1;
  private static final int DIALOG_NO_PHONE_NAME = 2;
  
  public static int lang = 0;
    
    private ServiceConnector mConnector = new ServiceConnector();
    private BackgroundUploader mUploadService = null;

    private class BackgroundUploaderConnectionListener implements ServiceListener<BackgroundUploader> {
    public void onConnect(BackgroundUploader uploadService) {
      Log.i(TAG, "onServiceConnected");
      mUploadService = uploadService;
    }
    
    public void onDisconnect(BackgroundUploader uploadService) {
      Log.i(TAG, "onServiceDisconnected");
      mUploadService = null;
    }
    }
    
    private class CredentialsValidatedListener implements ValidationListener {
      /**
         * Called when CheckCredentialsTask completes
         */
      public void onValidationComplete(int validationResult) {
        if (validationResult == CheckCredentialsTask.CREDENTIALS_NO_CONNECTION) {
          // TODO(XXX) Do not showDialog if the activity is not present
          Log.i(TAG, "Cannot validate EMR credentials - no network connectivity!");
          showDialog(DIALOG_NO_CONNECTIVITY);
        } else if (validationResult == CheckCredentialsTask.CREDENTIALS_INVALID) {
          Log.i(TAG, "Could not validate EMR username/password");
          if (mUploadService != null) {
            mUploadService.onCredentialsChanged(false);
          }
          showDialog(DIALOG_INCORRECT_PASSWORD);
        } else if (validationResult == CheckCredentialsTask.CREDENTIALS_VALID) {
          Log.i(TAG, "Username/Password for EMR correct");
          if (mUploadService != null) {
            mUploadService.onCredentialsChanged(true);
          }
        }
      }
    }

  @Override
  public void onDestroy() {
    super.onDestroy();
    try {
      mConnector.disconnect(this);
      mUploadService = null;
    
    } catch (IllegalArgumentException e) {
      Log.e(TAG, "While disconnecting service got exception: " + e.toString());
      e.printStackTrace();
    }
  }
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        if(lang==0)//English
        {
        setContentView(R.layout.main);
     
       View openProcedure = findViewById(R.id.moca_main_procedure);
        
        openProcedure.setOnClickListener(this);

        View viewTransfers = findViewById(R.id.moca_main_transfers);
        viewTransfers.setOnClickListener(this);

        View viewNotifications = findViewById(R.id.moca_main_notifications);
        viewNotifications.setOnClickListener(this);
        }
        else // devanagri
        {
          setContentView(R.layout.main_devanagri);
        
             
             Button myButton1 = (Button) findViewById(R.id.moca_main_procedure);
             myButton1.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/mangal.ttf"));
            View openProcedure = findViewById(R.id.moca_main_procedure);
             
             openProcedure.setOnClickListener(this);
             Button myButton2 = (Button) findViewById(R.id.moca_main_transfers);
             myButton2.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/mangal.ttf"));
             
             View viewTransfers = findViewById(R.id.moca_main_transfers);
             viewTransfers.setOnClickListener(this);
             Button myButton3 = (Button) findViewById(R.id.moca_main_notifications);
             myButton3.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/mangal.ttf"));
             View viewNotifications = findViewById(R.id.moca_main_notifications);
             viewNotifications.setOnClickListener(this);
          
        }
        // Create a connection to the background upload service. This starts the service when the app starts.
        try {
          mConnector.setServiceListener(new BackgroundUploaderConnectionListener());
          mConnector.connect(this);
        }
        catch (Exception e) {
          Log.e(TAG, "Exception starting background upload service: " + e.toString());
          e.printStackTrace();
        }
    }

    private void pickProcedure() {
      Intent i = new Intent(Intent.ACTION_PICK);
        i.setType(ProcedureSQLFormat.CONTENT_TYPE);
        i.setData(ProcedureSQLFormat.CONTENT_URI);
        startActivityForResult(i, PICK_PROCEDURE);
    }
    
    private void pickSavedProcedure() {
      Intent i = new Intent(Intent.ACTION_PICK);
      i.setType(SavedProcedureSQLFormat.CONTENT_TYPE);
      i.setData(SavedProcedureSQLFormat.CONTENT_URI);
      startActivityForResult(i, PICK_SAVEDPROCEDURE);
    }
    
    private void pickNotification() {
        Intent i = new Intent(Intent.ACTION_PICK);
        i.setType(NotificationSQLFormat.CONTENT_TYPE);
        i.setData(NotificationSQLFormat.CONTENT_URI);
        startActivityForResult(i, PICK_NOTIFICATION);
    }
    
    @Override
    public void onClick(View arg0) {
    switch (arg0.getId()) {
    case R.id.moca_main_procedure:
      pickProcedure();
      break;
    case R.id.moca_main_transfers:
      pickSavedProcedure();
      break;
    case R.id.moca_main_notifications:
      pickNotification();
      break;
    }
  }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        
        switch (resultCode) {
        case RESULT_CANCELED:
          Log.i(TAG, "onActivityResult: requestCode=" + requestCode + " resultCode=CANCELLED");
          if(requestCode == RUN_PROCEDURE) {
            pickProcedure();
          } else if(requestCode == RESUME_PROCEDURE) {
            pickSavedProcedure();
          } else if(requestCode == SETTINGS) {
            //Check to make sure there is a phone number entered, otherwise will not connect to MDS
            String phoneNum = PreferenceManager.getDefaultSharedPreferences(this).getString(Constants.PREFERENCE_PHONE_NAME, null);
            Log.i(TAG, "phoneNum from preferences is: " + phoneNum);
            if (phoneNum == null || phoneNum.equals("")) {
              Log.i(TAG, "No phone number entered - showing dialog now");
              showDialog(DIALOG_NO_PHONE_NAME);
            }
            
            // Attempt to validate the credentials changed in the settings.
            CheckCredentialsTask checkCredentialsTask = new CheckCredentialsTask();
            checkCredentialsTask.setValidationListener(new CredentialsValidatedListener());
            checkCredentialsTask.execute(this);

          }
            break;
        case RESULT_OK:
          Uri uri = null;
          if(data != null) {
            uri = data.getData();
          }
          Log.i(TAG, "onActivityResult: requestCode=" + requestCode + " resultCode=OK uri=" + (uri == null ?  "(null)" : uri.toString()));
          
          if(requestCode == PICK_PROCEDURE) {
            assert(uri != null);
            doPerformProcedure(uri);
          } else if(requestCode == PICK_SAVEDPROCEDURE) {
            assert(uri != null);
            doResumeProcedure(uri);
          } else if(requestCode == PICK_NOTIFICATION) {
            assert(uri != null);
            doShowNotification(uri);
          } else if (requestCode == RUN_PROCEDURE || requestCode == RESUME_PROCEDURE) {
            pickSavedProcedure();
          }
            break;
        }
    }
   
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DIALOG_INCORRECT_PASSWORD:
          return new AlertDialog.Builder(this)
          .setTitle("Error!")
            .setMessage("OpenMRS username/password incorrect!")
            .setPositiveButton("Change Settings", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                  // Dismiss dialog and return to settings                  
                  Intent i = new Intent(Intent.ACTION_PICK);
                    i.setClass(Moca.this, Settings.class);
                    startActivityForResult(i, SETTINGS);
                  setResult(RESULT_OK, null);
                  dialog.dismiss();
                }
            })
            .setCancelable(true)
            .setNegativeButton("Cancel", new OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
          setResult(RESULT_CANCELED, null);
                  dialog.dismiss();
        }
            })
            .create();
        case DIALOG_NO_CONNECTIVITY:
          return new AlertDialog.Builder(this)
          .setTitle("Error!")
            .setMessage("Could not check username/password - no network connection.")
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                  // Dismiss dialog and return to settings
                  setResult(RESULT_OK, null);
                  dialog.dismiss();
                }
            })
            .create();
        case DIALOG_NO_PHONE_NAME:
          return new AlertDialog.Builder(this)
          .setTitle("Error!")
            .setMessage("Phone Name is blank! Must enter a phone number")
            .setPositiveButton("Change Settings", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                  // Dismiss dialog and return to settings                  
                  Intent i = new Intent(Intent.ACTION_PICK);
                    i.setClass(Moca.this, Settings.class);
                    startActivityForResult(i, SETTINGS);
                  setResult(RESULT_OK, null);
                  dialog.dismiss();
                }
            })
            .setCancelable(true)
            .setNegativeButton("Cancel", new OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
          setResult(RESULT_CANCELED, null);
                  dialog.dismiss();
        }
            })
            .create();
       
        
        }
        return null;
    }

    private void doShowNotification(Uri uri) {
      try {
        Intent i = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(i);
      } catch(Exception e) {
        Log.e(TAG, "While showing notification " + uri + " an exception occured: " + e.toString());
      }
    }
    
    private void doResumeProcedure(Uri uri) {
      try {
        Intent i = new Intent(Intent.ACTION_VIEW, uri);
        i.putExtra("savedProcedureUri", uri.toString());
        startActivityForResult(i, RESUME_PROCEDURE);
      } catch(Exception e) {
        Log.e(TAG, "While resuming procedure " + uri + " an exception occured: " + e.toString());
      }
    }
    
    private void doPerformProcedure(final Uri uri) {
        Log.i(TAG, "doPerformProcedure uri=" + uri.toString());
        try {
          Intent i = new Intent(Intent.ACTION_VIEW, uri);
        startActivityForResult(i, RUN_PROCEDURE);
        } catch (Exception e) {
            MocaUtil.errorAlert(this, e.toString());
            Log.e(TAG, "While running procedure " + uri + " an exception occured: " + e.toString());
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
      super.onCreateOptionsMenu(menu);
        if(lang==0)
      {
        menu.add(0, OPTION_RELOAD_DATABASE, 0, "Reload Database");
        menu.add(0, OPTION_SETTINGS, 1, "Settings");
    menu.add(0, OPTION_SYNC, 2, "Sync");    menu.add(0, OPTION_LANGUAGE, 3, "Marathi");
      }
        else
        {   
            
          menu.add(0, OPTION_RELOAD_DATABASE, 0, "*  0  $   2  K  !     0  >   ");
            menu.add(0, OPTION_SETTINGS, 1, "8  G    ?      M  8   ");
        menu.add(0, OPTION_SYNC, 2, "8  ?      M  0  (  >      <  ");
        menu.add(0, OPTION_LANGUAGE, 3, "      M  2  ?  6   ");
        }
        return true;
    }
    
    private void doClearDatabase() {
      // TODO: context leak
      new ResetDatabaseTask(this).execute(this);
    }
    
    private void doUpdatePatientDatabase() {
      // TODO: context leak
      new MDSSyncTask(this).execute(this);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()) {
        case OPTION_RELOAD_DATABASE:
          // TODO: Dialog leak
          AlertDialog.Builder bldr = new AlertDialog.Builder(this);
          AlertDialog dialog = bldr.create();
          if(lang==0)
          dialog.setMessage("Warning: reloading the database will clear all saved procedures and notifications. Proceed?");
          else
            
          if(lang==1)
            dialog.setMessage("9  G     G  2  M  /  >   (  G   8  0  M  5   !  >    >     K  0  >   9  K    2    ");  
          if(lang==0)
          {dialog.setCancelable(true);
           dialog.setButton("Yes", new OnClickListener() {
            public void onClick(DialogInterface i, int v) {
              doClearDatabase();
            }
          });
          dialog.setButton2("No", (OnClickListener)null);
          dialog.show();
          }
          else
          {dialog.setCancelable(true);
          dialog.setButton("9  K  ", new OnClickListener() {
           public void onClick(DialogInterface i, int v) {
             doClearDatabase();
           }
         });
         dialog.setButton2("(    K  ", (OnClickListener)null);
         dialog.show();
         }
          return true;
        case OPTION_SETTINGS:
            Intent i = new Intent(Intent.ACTION_PICK);
            i.setClass(this, Settings.class);
            startActivityForResult(i, SETTINGS);
            return true;
        case OPTION_SYNC:
          doUpdatePatientDatabase();
      return true;
        case OPTION_LANGUAGE:
           if(lang ==1)
           lang = 0;
           else
           if(lang==0)
            lang=1; 
           
           Intent i1 = new Intent(Intent.ACTION_MAIN);  
           i1.setClass(this, Moca.class);
           startActivity(i1);
            return true;
        
        
        
        }
        
          
        return false;
    }
   
}




Java Source Code List

.Moca.java
org.moca.Constants.java
org.moca.ImagePreviewDialog.java
org.moca.ScalingImageAdapter.java
org.moca.SelectableImageView.java
org.moca.activity.NotificationList.java
org.moca.activity.NotificationViewer.java
org.moca.activity.PatientInfoDialog.java
org.moca.activity.ProcedureRunner.java
org.moca.activity.ProceduresList.java
org.moca.activity.SavedProcedureList.java
org.moca.activity.Settings.java
org.moca.db.EncounterDAO.java
org.moca.db.EventDAO.java
org.moca.db.EventProvider.java
org.moca.db.Event.java
org.moca.db.ImageProvider.java
org.moca.db.MocaDB.java
org.moca.db.NotificationMessage.java
org.moca.db.NotificationProvider.java
org.moca.db.PatientInfo.java
org.moca.db.PatientProvider.java
org.moca.db.PatientValidator.java
org.moca.db.ProcedureDAO.java
org.moca.db.ProcedureProvider.java
org.moca.db.SavedProcedureProvider.java
org.moca.db.SoundProvider.java
org.moca.media.AudioPlayer.java
org.moca.net.MDSCode.java
org.moca.net.MDSInterface.java
org.moca.net.MDSNotification.java
org.moca.net.MDSResult.java
org.moca.net.SMSReceive.java
org.moca.procedure.BinaryUploadElement.java
org.moca.procedure.DateElement.java
org.moca.procedure.GpsElement.java
org.moca.procedure.MultiSelectElement.java
org.moca.procedure.PatientIdElement.java
org.moca.procedure.PictureElement.java
org.moca.procedure.ProcedureElement.java
org.moca.procedure.ProcedurePage.java
org.moca.procedure.ProcedureParseException.java
org.moca.procedure.Procedure.java
org.moca.procedure.RadioElement.java
org.moca.procedure.SelectElement.java
org.moca.procedure.SoundElement.java
org.moca.procedure.TextElement.java
org.moca.procedure.TextEntryElement.java
org.moca.procedure.ValidationError.java
org.moca.procedure.branching.Criteria.java
org.moca.procedure.branching.Criterion.java
org.moca.procedure.branching.LogicAnd.java
org.moca.procedure.branching.LogicBase.java
org.moca.procedure.branching.LogicNot.java
org.moca.procedure.branching.LogicOr.java
org.moca.service.BackgroundUploader.java
org.moca.service.QueueManager.java
org.moca.service.ServiceConnector.java
org.moca.service.ServiceListener.java
org.moca.task.CheckCredentialsTask.java
org.moca.task.ImageProcessingTaskRequest.java
org.moca.task.ImageProcessingTask.java
org.moca.task.MDSSyncTask.java
org.moca.task.PatientLookupListener.java
org.moca.task.PatientLookupTask.java
org.moca.task.ResetDatabaseTask.java
org.moca.task.ValidationListener.java
org.moca.util.MocaUtil.java
org.moca.util.UserDatabase.java