示例#1
0
 /** Handle TrytonCall feedback. */
 @SuppressWarnings("unchecked")
 public boolean handleMessage(Message msg) {
   // Process message
   switch (msg.what) {
     case TrytonCall.CALL_SAVE_OK:
       this.callId = 0;
       this.hideLoadingDialog();
       Toast t = Toast.makeText(this, R.string.data_send_done, Toast.LENGTH_SHORT);
       t.show();
       // Update data with fresh one
       Model m = (Model) msg.obj;
       if (Session.current.isCreatingModel()) {
         this.postCreate(m);
       } else {
         this.postUpdate(m);
       }
       break;
     case TrytonCall.CALL_DELETE_OK:
       this.callId = 0;
       this.hideLoadingDialog();
       this.postDelete();
       break;
     case TrytonCall.CALL_SAVE_NOK:
     case TrytonCall.CALL_DELETE_NOK:
     case DataLoader.DATA_NOK:
     case DataLoader.VIEWS_NOK:
       this.callId = 0;
       this.hideLoadingDialog();
       Exception e = (Exception) msg.obj;
       if (!AlertBuilder.showUserError(e, this) && !AlertBuilder.showUserError(e, this)) {
         if (Configure.getOfflineUse(this)) {
           // Generic error, queue call and continue
           t = Toast.makeText(this, R.string.data_send_queued, Toast.LENGTH_SHORT);
           t.show();
           if (msg.what == TrytonCall.CALL_DELETE_NOK) {
             this.queueDelete();
           } else {
             if (!Session.current.isCreatingModel()) {
               this.queueUpdate();
             } else {
               this.queueCreate();
             }
           }
         } else {
           // Show the error
           AlertDialog.Builder b = new AlertDialog.Builder(this);
           b.setTitle(R.string.error);
           b.setMessage(R.string.network_error);
           ((Exception) msg.obj).printStackTrace();
           b.show();
         }
       }
       break;
     case DataLoader.VIEWS_OK:
       this.callId = 0;
       this.view = (ModelView) ((Object[]) msg.obj)[1];
       this.loadDataAndMeta();
       break;
     case DataLoader.RELFIELDS_OK:
       this.callId = 0;
       this.relFields = (List<RelField>) ((Object[]) msg.obj)[1];
       this.loadData();
       break;
     case DataLoader.DATA_OK:
       this.callId = 0;
       List<Model> dataList = (List<Model>) ((Object[]) msg.obj)[1];
       if (dataList.size() > 0) {
         // Refresh edited model (not done when creating
         // as data is not loaded)
         Session.current.updateEditedModel(dataList.get(0));
       }
       this.initView();
       this.refreshDisplay();
       this.hideLoadingDialog();
       break;
     case TrytonCall.NOT_LOGGED:
       this.callId = 0;
       // Ask for relog
       this.lastFailMessage = (Integer) msg.obj;
       this.hideLoadingDialog();
       AlertBuilder.showRelog(this, new Handler(this));
       break;
     case AlertBuilder.RELOG_CANCEL:
       if (this.lastFailMessage == TrytonCall.CALL_DELETE_NOK) {
         // Nothing
       } else if (this.lastFailMessage == TrytonCall.CALL_SAVE_NOK) {
         // Nothing
       } else {
         this.finish();
       }
       break;
     case AlertBuilder.RELOG_OK:
       if (this.lastFailMessage == TrytonCall.CALL_DELETE_NOK) {
         this.sendDelete();
       } else if (this.lastFailMessage == TrytonCall.CALL_SAVE_NOK) {
         this.sendSave();
       } else {
         this.loadViewAndData();
       }
       break;
   }
   return true;
 }