Home | History | Annotate | Download | only in projection
      1 /*
      2  * Copyright (C) 2014 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 
     18 package com.android.cts.verifier.projection;
     19 
     20 import android.content.ComponentName;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.content.ServiceConnection;
     24 import android.graphics.SurfaceTexture;
     25 import android.os.Bundle;
     26 import android.os.Handler;
     27 import android.os.IBinder;
     28 import android.os.RemoteException;
     29 import android.util.DisplayMetrics;
     30 import android.util.Log;
     31 import android.view.MotionEvent;
     32 import android.view.Surface;
     33 import android.view.TextureView;
     34 import android.view.View;
     35 import android.view.View.OnTouchListener;
     36 
     37 import com.android.cts.verifier.PassFailButtons;
     38 import com.android.cts.verifier.R;
     39 
     40 /**
     41  * Base activity for each projection test case. Handles the service connection and TextureView
     42  * listeners
     43  */
     44 public abstract class ProjectionActivity extends PassFailButtons.Activity
     45         implements TextureView.SurfaceTextureListener {
     46     private static final String TAG = ProjectionActivity.class.getSimpleName();
     47     protected Intent mStartIntent;
     48     protected TextureView mTextureView;
     49     protected volatile Surface mSurface;
     50     protected int mWidth;
     51     protected int mHeight;
     52     protected ProjectionPresentationType mType;
     53 
     54     protected IProjectionService mService;
     55     protected final ServiceConnection mConnection = new ServiceConnection() {
     56 
     57         @Override
     58         public void onServiceConnected(ComponentName name, IBinder binder) {
     59             mService = IProjectionService.Stub.asInterface(binder);
     60             new Handler().post(new Runnable() {
     61 
     62                 @Override
     63                 public void run() {
     64                     Log.i(TAG, "onServiceConnected thread " + Thread.currentThread());
     65                     DisplayMetrics metrics = ProjectionActivity.this.getResources().getDisplayMetrics();
     66                     try {
     67                         mService.startRendering(mSurface, mWidth, mHeight, metrics.densityDpi, mType.ordinal());
     68                     } catch (RemoteException e) {
     69                         Log.e(TAG, "Failed to execute startRendering", e);
     70                     }
     71                 }
     72 
     73             });
     74 
     75         }
     76 
     77         @Override
     78         public void onServiceDisconnected(ComponentName name) {
     79             mService = null;
     80         }
     81 
     82     };
     83 
     84     @Override
     85     protected void onCreate(Bundle savedInstanceState) {
     86         super.onCreate(savedInstanceState);
     87         Log.i(TAG, "onCreate");
     88         mStartIntent = new Intent(this, ProjectionService.class);
     89 
     90     }
     91 
     92     protected View setContentViewAndInfoResources(int layoutId, int titleId, int infoId) {
     93         View view = getLayoutInflater().inflate(layoutId, null);
     94         setContentView(view);
     95 
     96         mTextureView = (TextureView) view.findViewById(R.id.texture_view);
     97         mTextureView.setSurfaceTextureListener(this);
     98         mTextureView.setOnTouchListener(new OnTouchListener() {
     99 
    100             @Override
    101             public boolean onTouch(View view, MotionEvent event) {
    102                 if (mService != null) {
    103                     try {
    104                         mService.onTouchEvent(event);
    105                     } catch (RemoteException e) {
    106                         Log.e(TAG, "Failed to execute onTouchEvent", e);
    107                     }
    108                 }
    109                 return true;
    110             }
    111 
    112         });
    113 
    114         setInfoResources(titleId, infoId, -1);
    115         setPassFailButtonClickListeners();
    116         return view;
    117     }
    118 
    119     @Override
    120     public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
    121         Log.i(TAG, "onSurfaceTextureAvailable " + "w: " + width + " h: " + height);
    122         mSurface = new Surface(surface);
    123         mWidth = width;
    124         mHeight = height;
    125         if (mService == null) {
    126             bindService(mStartIntent, mConnection, Context.BIND_AUTO_CREATE);
    127         }
    128 
    129     }
    130 
    131     @Override
    132     public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
    133         Log.i(TAG, "onSurfaceTextureDestroyed");
    134         if (mService != null) {
    135             try {
    136                 mService.stopRendering();
    137             } catch (RemoteException e) {
    138                 Log.e(TAG, "Failed to execute stopRendering", e);
    139             }
    140         }
    141         mSurface.release();
    142         mSurface = null;
    143 
    144         unbindService(mConnection);
    145         mService = null;
    146         return true;
    147     }
    148 
    149     @Override
    150     public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
    151         Log.i(TAG, "onSurfaceTextureSizeChanged " + surface.toString() + "w: " + width + " h: "
    152                 + height);
    153         mWidth = width;
    154         mHeight = height;
    155     }
    156 
    157     @Override
    158     public void onSurfaceTextureUpdated(SurfaceTexture surface) {
    159         Log.i(TAG, "onSurfaceTextureUpdated " + surface.toString());
    160     }
    161 
    162 }
    163