Sync sample prebuilts for mnc-dev

Synced with /developers/samples/android commit
415e5ce8ad7128bed20c28e923f2f91bbfff46a9.

Change-Id: I8716d051213210ec991fb692f79848a9f086a52c
This commit is contained in:
Trevor Johns
2015-05-27 13:18:28 -07:00
parent c75eb5eeed
commit 296083e2fc
212 changed files with 10006 additions and 1 deletions

View File

@@ -0,0 +1,135 @@
/*
* Copyright 2015 The Android Open Source Project
*
* 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.example.android.basicpermissions;
import com.example.android.basicpermissions.camera.CameraPreviewActivity;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
/**
* Launcher Activity that demonstrates the use of runtime permissions for Android M.
* This Activity requests permissions to access the camera
* ({@link android.Manifest.permission#CAMERA})
* when the 'Show Camera Preview' button is clicked to start {@link CameraPreviewActivity} once
* the permission has been granted.
* <p>
* First, the status of the Camera permission is checked using {@link
* Activity#checkSelfPermission(String)}.
* If it has not been granted ({@link PackageManager#PERMISSION_GRANTED}), it is requested by
* calling
* {@link Activity#requestPermissions(String[], int)}. The result of the request is returned in
* {@link Activity#onRequestPermissionsResult(int, String[], int[])}, which starts {@link
* CameraPreviewActivity}
* if the permission has been granted.
*/
public class MainActivity extends Activity {
private static final int PERMISSION_REQUEST_CAMERA = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Register a listener for the 'Show Camera Preview' button.
Button b = (Button) findViewById(R.id.button_open_camera);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showCameraPreview();
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
// BEGIN_INCLUDE(onRequestPermissionsResult)
if (requestCode == PERMISSION_REQUEST_CAMERA) {
// Request for camera permission.
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission has been granted. Start camera preview Activity.
Toast.makeText(this, "Camera permission was granted. Starting preview.",
Toast.LENGTH_SHORT)
.show();
startCamera();
} else {
// Permission request was denied.
Toast.makeText(this, "Camera permission request was denied.", Toast.LENGTH_SHORT)
.show();
}
}
// END_INCLUDE(onRequestPermissionsResult)
}
private void showCameraPreview() {
// BEGIN_INCLUDE(startCamera)
if (isMNC()) {
// On Android M and above, need to check if permission has been granted at runtime.
if (checkSelfPermission(Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED) {
// Permission is available, start camera preview
startCamera();
Toast.makeText(this,
"Camera permission has already been granted. Starting preview.",
Toast.LENGTH_SHORT).show();
} else {
// Permission has not been granted and must be requested.
Toast.makeText(this,
"Permission is not available. Requesting camera permission.",
Toast.LENGTH_SHORT).show();
requestPermissions(new String[]{Manifest.permission.CAMERA},
PERMISSION_REQUEST_CAMERA);
}
} else {
/*
Below Android M all permissions have already been grated at install time and do not
need to verified or requested.
If a permission has been disabled in the system settings, the API will return
unavailable or empty data instead. */
Toast.makeText(this,
"Requested permissions are granted at install time below M and are always "
+ "available at runtime.",
Toast.LENGTH_SHORT).show();
startCamera();
}
// END_INCLUDE(startCamera)
}
private void startCamera() {
Intent intent = new Intent(this, CameraPreviewActivity.class);
startActivity(intent);
}
public static boolean isMNC() {
/*
TODO: In the Android M Preview release, checking if the platform is M is done through
the codename, not the version code. Once the API has been finalised, the following check
should be used: */
// return Build.VERSION.SDK_INT >= Build.VERSION_CODES.MNC
return "MNC".equals(Build.VERSION.CODENAME);
}
}

View File

@@ -0,0 +1,142 @@
/*
* Copyright 2015 The Android Open Source Project
*
* 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.example.android.basicpermissions.camera;
import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.IOException;
/**
* Camera preview that displays a {@link Camera}.
*
* Handles basic lifecycle methods to display and stop the preview.
* <p>
* Implementation is based directly on the documentation at
* http://developer.android.com/guide/topics/media/camera.html
*/
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "CameraPreview";
private SurfaceHolder mHolder;
private Camera mCamera;
private Camera.CameraInfo mCameraInfo;
private int mDisplayOrientation;
public CameraPreview(Context context, Camera camera, Camera.CameraInfo cameraInfo,
int displayOrientation) {
super(context);
// Do not initialise if no camera has been set
if (camera == null || cameraInfo == null) {
return;
}
mCamera = camera;
mCameraInfo = cameraInfo;
mDisplayOrientation = displayOrientation;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
Log.d(TAG, "Camera preview started.");
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null) {
// preview surface does not exist
Log.d(TAG, "Preview surface does not exist");
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
Log.d(TAG, "Preview stopped.");
} catch (Exception e) {
// ignore: tried to stop a non-existent preview
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
int orientation = calculatePreviewOrientation(mCameraInfo, mDisplayOrientation);
mCamera.setDisplayOrientation(orientation);
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
Log.d(TAG, "Camera preview started.");
} catch (Exception e) {
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
/**
* Calculate the correct orientation for a {@link Camera} preview that is displayed on screen.
*
* Implementation is based on the sample code provided in
* {@link Camera#setDisplayOrientation(int)}.
*/
public static int calculatePreviewOrientation(Camera.CameraInfo info, int rotation) {
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
return result;
}
}

View File

@@ -0,0 +1,104 @@
/*
* Copyright 2015 The Android Open Source Project
*
* 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.example.android.basicpermissions.camera;
import com.example.android.basicpermissions.R;
import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.Toast;
/**
* Displays a {@link CameraPreview} of the first {@link Camera}.
* An error message is displayed if the Camera is not available.
* <p>
* This Activity is only used to illustrate that access to the Camera API has been granted (or
* denied) as part of the runtime permissions model. It is not relevant for the use of the
* permissions API.
* <p>
* Implementation is based directly on the documentation at
* http://developer.android.com/guide/topics/media/camera.html
*/
public class CameraPreviewActivity extends Activity {
private static final String TAG = "CameraPreview";
/**
* Id of the camera to access. 0 is the first camera.
*/
private static final int CAMERA_ID = 0;
private CameraPreview mPreview;
private Camera mCamera;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Open an instance of the first camera and retrieve its info.
mCamera = getCameraInstance(CAMERA_ID);
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
Camera.getCameraInfo(CAMERA_ID, cameraInfo);
if (mCamera == null || cameraInfo == null) {
// Camera is not available, display error message
Toast.makeText(this, "Camera is not available.", Toast.LENGTH_SHORT).show();
setContentView(R.layout.activity_camera_unavailable);
} else {
setContentView(R.layout.activity_camera);
// Get the rotation of the screen to adjust the preview image accordingly.
final int displayRotation = getWindowManager().getDefaultDisplay()
.getRotation();
// Create the Preview view and set it as the content of this Activity.
mPreview = new CameraPreview(this, mCamera, cameraInfo, displayRotation);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
}
}
@Override
public void onPause() {
super.onPause();
// Stop camera access
releaseCamera();
}
/** A safe way to get an instance of the Camera object. */
private Camera getCameraInstance(int cameraId) {
Camera c = null;
try {
c = Camera.open(cameraId); // attempt to get a Camera instance
} catch (Exception e) {
// Camera is not available (in use or does not exist)
Toast.makeText(this, "Camera " + cameraId + " is not available: " + e.getMessage(),
Toast.LENGTH_SHORT).show();
}
return c; // returns null if camera is unavailable
}
private void releaseCamera() {
if (mCamera != null) {
mCamera.release(); // release the camera for other applications
mCamera = null;
}
}
}