Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2015 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.benchmark.ui;
     18 
     19 import android.animation.ObjectAnimator;
     20 import android.animation.ValueAnimator;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.graphics.Bitmap;
     24 import android.graphics.Canvas;
     25 import android.graphics.Color;
     26 import android.graphics.Rect;
     27 import android.os.Bundle;
     28 import android.support.v7.app.AppCompatActivity;
     29 import android.util.AttributeSet;
     30 import android.util.DisplayMetrics;
     31 import android.view.MotionEvent;
     32 import android.view.View;
     33 
     34 import com.android.benchmark.R;
     35 import com.android.benchmark.ui.automation.Automator;
     36 import com.android.benchmark.ui.automation.Interaction;
     37 
     38 /**
     39  *
     40  */
     41 public class BitmapUploadActivity extends AppCompatActivity {
     42     private Automator mAutomator;
     43 
     44     public static class UploadView extends View {
     45         private int mColorValue;
     46         private Bitmap mBitmap;
     47         private final DisplayMetrics mMetrics = new DisplayMetrics();
     48         private final Rect mRect = new Rect();
     49 
     50         public UploadView(Context context, AttributeSet attrs) {
     51             super(context, attrs);
     52         }
     53 
     54         @SuppressWarnings("unused")
     55         public void setColorValue(int colorValue) {
     56             if (colorValue == mColorValue) return;
     57 
     58             mColorValue = colorValue;
     59 
     60             // modify the bitmap's color to ensure it's uploaded to the GPU
     61             mBitmap.eraseColor(Color.rgb(mColorValue, 255 - mColorValue, 255));
     62 
     63             invalidate();
     64         }
     65 
     66         @Override
     67         protected void onAttachedToWindow() {
     68             super.onAttachedToWindow();
     69 
     70             getDisplay().getMetrics(mMetrics);
     71             int minDisplayDimen = Math.min(mMetrics.widthPixels, mMetrics.heightPixels);
     72             int bitmapSize = Math.min((int) (minDisplayDimen * 0.75), 720);
     73             if (mBitmap == null
     74                     || mBitmap.getWidth() != bitmapSize
     75                     || mBitmap.getHeight() != bitmapSize) {
     76                 mBitmap = Bitmap.createBitmap(bitmapSize, bitmapSize, Bitmap.Config.ARGB_8888);
     77             }
     78         }
     79 
     80         @Override
     81         protected void onDraw(Canvas canvas) {
     82             if (mBitmap != null) {
     83                 mRect.set(0, 0, getWidth(), getHeight());
     84                 canvas.drawBitmap(mBitmap, null, mRect, null);
     85             }
     86         }
     87 
     88         @Override
     89         public boolean onTouchEvent(MotionEvent event) {
     90             // animate color to force bitmap uploads
     91             return super.onTouchEvent(event);
     92         }
     93     }
     94 
     95     @Override
     96     protected void onCreate(Bundle savedInstanceState) {
     97         super.onCreate(savedInstanceState);
     98         setContentView(R.layout.activity_bitmap_upload);
     99 
    100         final View uploadRoot = findViewById(R.id.upload_root);
    101         uploadRoot.setKeepScreenOn(true);
    102         uploadRoot.setOnTouchListener(new View.OnTouchListener() {
    103             @Override
    104             public boolean onTouch(View view, MotionEvent motionEvent) {
    105                 UploadView uploadView = (UploadView) findViewById(R.id.upload_view);
    106                 ObjectAnimator colorValueAnimator =
    107                         ObjectAnimator.ofInt(uploadView, "colorValue", 0, 255);
    108                 colorValueAnimator.setRepeatMode(ValueAnimator.REVERSE);
    109                 colorValueAnimator.setRepeatCount(100);
    110                 colorValueAnimator.start();
    111 
    112                 // animate scene root to guarantee there's a minimum amount of GPU rendering work
    113                 ObjectAnimator yAnimator = ObjectAnimator.ofFloat(
    114                         view, "translationY", 0, 100);
    115                 yAnimator.setRepeatMode(ValueAnimator.REVERSE);
    116                 yAnimator.setRepeatCount(100);
    117                 yAnimator.start();
    118 
    119                 return true;
    120             }
    121         });
    122 
    123         final UploadView uploadView = (UploadView) findViewById(R.id.upload_view);
    124         final int runId = getIntent().getIntExtra("com.android.benchmark.RUN_ID", 0);
    125         final int iteration = getIntent().getIntExtra("com.android.benchmark.ITERATION", -1);
    126 
    127         mAutomator = new Automator("BMUpload", runId, iteration, getWindow(),
    128                 new Automator.AutomateCallback() {
    129                     @Override
    130                     public void onPostAutomate() {
    131                         Intent result = new Intent();
    132                         setResult(RESULT_OK, result);
    133                         finish();
    134                     }
    135 
    136                     @Override
    137                     public void onAutomate() {
    138                         int[] coordinates = new int[2];
    139                         uploadRoot.getLocationOnScreen(coordinates);
    140 
    141                         int x = coordinates[0];
    142                         int y = coordinates[1];
    143 
    144                         float width = uploadRoot.getWidth();
    145                         float height = uploadRoot.getHeight();
    146 
    147                         float middleX = (x + width) / 5;
    148                         float middleY = (y + height) / 5;
    149 
    150                         addInteraction(Interaction.newTap(middleX, middleY));
    151                     }
    152                 });
    153 
    154         mAutomator.start();
    155     }
    156 
    157 }
    158