content: /*
   *    Copyright (C) 2017 MINDORKS NEXTGEN PRIVATE LIMITED
   *
   *    Licensed under the Apache License, Version 2.0 (the "License");
   *    you may not use this file except in compliance with the License.
   *    You may obtain a copy of the License at
   *
   *        http://www.apache.org/licenses/LICENSE-2.0
   *
   *    Unless required by applicable law or agreed to in writing, software
   *    distributed under the License is distributed on an "AS IS" BASIS,
   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *    See the License for the specific language governing permissions and
   *    limitations under the License.
   */
  
  package com.mindorks.tensorflowexample;
  
  import android.graphics.PointF;
  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.util.Log;
  import android.view.MotionEvent;
  import android.view.View;
  import android.widget.TextView;
  
  import com.mindorks.tensorflowexample.view.DrawModel;
  import com.mindorks.tensorflowexample.view.DrawView;
  
  import java.util.List;
  import java.util.concurrent.Executor;
  import java.util.concurrent.Executors;
  
  
  public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
  
      private static final String TAG = "MainActivity";
  
      private static final int PIXEL_WIDTH = 28;
  
      private TextView mResultText;
  
      private float mLastX;
  
      private float mLastY;
  
      private DrawModel mModel;
      private DrawView mDrawView;
  
      private View detectButton;
  
      private PointF mTmpPoint = new PointF();
  
      private static final int INPUT_SIZE = 28;
      private static final String INPUT_NAME = "input";
      private static final String OUTPUT_NAME = "output";
  
      private static final String MODEL_FILE = "file:///android_asset/mnist_model_graph.pb";
      private static final String LABEL_FILE =
              "file:///android_asset/graph_label_strings.txt";
  
      private Classifier classifier;
      private Executor executor = Executors.newSingleThreadExecutor();
  
  
      @SuppressWarnings("SuspiciousNameCombination")
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          mModel = new DrawModel(PIXEL_WIDTH, PIXEL_WIDTH);
  
          mDrawView = (DrawView) findViewById(R.id.view_draw);
          mDrawView.setModel(mModel);
          mDrawView.setOnTouchListener(this);
  
          detectButton = findViewById(R.id.buttonDetect);
          detectButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  onDetectClicked();
              }
          });
  
          View clearButton = findViewById(R.id.buttonClear);
          clearButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  onClearClicked();
              }
          });
  
          mResultText = (TextView) findViewById(R.id.textResult);
  
          initTensorFlowAndLoadModel();
      }
  
      private void initTensorFlowAndLoadModel() {
          executor.execute(new Runnable() {
              @Override
              public void run() {
                  try {
                      classifier = TensorFlowImageClassifier.create(
                              getAssets(),
                              MODEL_FILE,
                              LABEL_FILE,
                              INPUT_SIZE,
                              INPUT_NAME,
                              OUTPUT_NAME);
                      makeButtonVisible();
                      Log.d(TAG, "Load Success");
                  } catch (final Exception e) {
                      throw new RuntimeException("Error initializing TensorFlow!", e);
                  }
              }
          });
      }
  
      private void makeButtonVisible() {
          runOnUiThread(new Runnable() {
              @Override
              public void run() {
                  detectButton.setVisibility(View.VISIBLE);
              }
          });
      }
  
      @Override
      protected void onResume() {
          mDrawView.onResume();
          super.onResume();
      }
  
      @Override
      protected void onPause() {
          mDrawView.onPause();
          super.onPause();
      }
  
      @Override
      public boolean onTouch(View v, MotionEvent event) {
          int action = event.getAction() & MotionEvent.ACTION_MASK;
  
          if (action == MotionEvent.ACTION_DOWN) {
              processTouchDown(event);
              return true;
  
          } else if (action == MotionEvent.ACTION_MOVE) {
              processTouchMove(event);
              return true;
  
          } else if (action == MotionEvent.ACTION_UP) {
              processTouchUp();
              return true;
          }
          return false;
      }
  
      private void processTouchDown(MotionEvent event) {
          mLastX = event.getX();
          mLastY = event.getY();
          mDrawView.calcPos(mLastX, mLastY, mTmpPoint);
          float lastConvX = mTmpPoint.x;
          float lastConvY = mTmpPoint.y;
          mModel.startLine(lastConvX, lastConvY);
      }
  
      private void processTouchMove(MotionEvent event) {
          float x = event.getX();
          float y = event.getY();
  
          mDrawView.calcPos(x, y, mTmpPoint);
          float newConvX = mTmpPoint.x;
          float newConvY = mTmpPoint.y;
          mModel.addLineElem(newConvX, newConvY);
  
          mLastX = x;
          mLastY = y;
          mDrawView.invalidate();
      }
  
      private void processTouchUp() {
          mModel.endLine();
      }
  
      private void onDetectClicked() {
          float pixels[] = mDrawView.getPixelData();
  
          final List<Classifier.Recognition> results = classifier.recognizeImage(pixels);
  
          if (results.size() > 0) {
              String value = " Number is : " +results.get(0).getTitle();
              mResultText.setText(value);
          }
  
      }
  
      private void onClearClicked() {
          mModel.clear();
          mDrawView.reset();
          mDrawView.invalidate();
  
          mResultText.setText("");
      }
  
      @Override
      protected void onDestroy() {
          super.onDestroy();
          executor.execute(new Runnable() {
              @Override
              public void run() {
                  classifier.close();
              }
          });
      }
  }
  
  