Home | History | Annotate | Download | only in calculator2
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.calculator2;
     18 
     19 import android.support.v4.view.ViewPager;
     20 import android.view.KeyEvent;
     21 import android.view.View;
     22 import android.widget.Button;
     23 
     24 class EventListener implements View.OnKeyListener,
     25                                View.OnClickListener,
     26                                View.OnLongClickListener {
     27     Logic mHandler;
     28     ViewPager mPager;
     29 
     30     void setHandler(Logic handler, ViewPager pager) {
     31         mHandler = handler;
     32         mPager = pager;
     33     }
     34 
     35     @Override
     36     public void onClick(View view) {
     37         int id = view.getId();
     38         switch (id) {
     39         case R.id.del:
     40             mHandler.onDelete();
     41             break;
     42 
     43         case R.id.clear:
     44             mHandler.onClear();
     45             break;
     46 
     47         case R.id.equal:
     48             mHandler.onEnter();
     49             break;
     50 
     51         default:
     52             if (view instanceof Button) {
     53                 String text = ((Button) view).getText().toString();
     54                 if (text.length() >= 2) {
     55                     // add paren after sin, cos, ln, etc. from buttons
     56                     text += '(';
     57                 }
     58                 mHandler.insert(text);
     59                 if (mPager != null && mPager.getCurrentItem() == Calculator.ADVANCED_PANEL) {
     60                     mPager.setCurrentItem(Calculator.BASIC_PANEL);
     61                 }
     62             }
     63         }
     64     }
     65 
     66     @Override
     67     public boolean onLongClick(View view) {
     68         int id = view.getId();
     69         if (id == R.id.del) {
     70             mHandler.onClear();
     71             return true;
     72         }
     73         return false;
     74     }
     75 
     76     @Override
     77     public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
     78         int action = keyEvent.getAction();
     79 
     80         if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT ||
     81             keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
     82             boolean eat = mHandler.eatHorizontalMove(keyCode == KeyEvent.KEYCODE_DPAD_LEFT);
     83             return eat;
     84         }
     85 
     86         //Work-around for spurious key event from IME, bug #1639445
     87         if (action == KeyEvent.ACTION_MULTIPLE && keyCode == KeyEvent.KEYCODE_UNKNOWN) {
     88             return true; // eat it
     89         }
     90 
     91         //Calculator.log("KEY " + keyCode + "; " + action);
     92 
     93         if (keyEvent.getUnicodeChar() == '=') {
     94             if (action == KeyEvent.ACTION_UP) {
     95                 mHandler.onEnter();
     96             }
     97             return true;
     98         }
     99 
    100         if (keyCode != KeyEvent.KEYCODE_DPAD_CENTER &&
    101             keyCode != KeyEvent.KEYCODE_DPAD_UP &&
    102             keyCode != KeyEvent.KEYCODE_DPAD_DOWN &&
    103             keyCode != KeyEvent.KEYCODE_ENTER) {
    104             if (keyEvent.isPrintingKey() && action == KeyEvent.ACTION_UP) {
    105                 // Tell the handler that text was updated.
    106                 mHandler.onTextChanged();
    107             }
    108             return false;
    109         }
    110 
    111         /*
    112            We should act on KeyEvent.ACTION_DOWN, but strangely
    113            sometimes the DOWN event isn't received, only the UP.
    114            So the workaround is to act on UP...
    115            http://b/issue?id=1022478
    116          */
    117 
    118         if (action == KeyEvent.ACTION_UP) {
    119             switch (keyCode) {
    120             case KeyEvent.KEYCODE_ENTER:
    121             case KeyEvent.KEYCODE_DPAD_CENTER:
    122                 mHandler.onEnter();
    123                 break;
    124 
    125             case KeyEvent.KEYCODE_DPAD_UP:
    126                 mHandler.onUp();
    127                 break;
    128 
    129             case KeyEvent.KEYCODE_DPAD_DOWN:
    130                 mHandler.onDown();
    131                 break;
    132             }
    133         }
    134         return true;
    135     }
    136 }
    137