forked from android/wear-os-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds RuntimePermissionsWear sample to WearOS repo.
Bug: 129548613 Test: Manually tested. Change-Id: I153816348e839fe808d25cdbd3cc6bae69d7fc94
- Loading branch information
1 parent
58aec69
commit 1e3cbd0
Showing
101 changed files
with
3,328 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
# GOOGLE SAMPLE PACKAGING DATA | ||
# | ||
# This file is used by Google as part of our samples packaging process. | ||
# End users may safely ignore this file. It has no relevance to other systems. | ||
--- | ||
status: PUBLISHED | ||
technologies: [Android] | ||
categories: [Wearable, Permissions] | ||
languages: [Java] | ||
solutions: [Mobile] | ||
github: android-RuntimePermissionsWear | ||
level: INTERMEDIATE | ||
icon: screenshots/icon-web.png | ||
apiRefs: | ||
- android:android.support.v4.app.ActivityCompat | ||
- android:android.support.v7.app.AppCompatActivity | ||
- android:android.support.wearable.activity.WearableActivity | ||
- android:android.support.wearable.view.WatchViewStub | ||
- android:com.google.android.gms.common.api.GoogleApiClient | ||
- android:com.google.android.gms.wearable.CapabilityApi | ||
- android:com.google.android.gms.wearable.CapabilityInfo | ||
- android:com.google.android.gms.wearable.DataMap | ||
- android:com.google.android.gms.wearable.MessageApi | ||
- android:com.google.android.gms.wearable.MessageEvent | ||
- android:com.google.android.gms.wearable.Node | ||
- android:com.google.android.gms.wearable.Wearable | ||
- android:com.google.android.gms.wearable.WearableListenerService | ||
license: apache2 |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
|
||
buildscript { | ||
repositories { | ||
google() | ||
jcenter() | ||
} | ||
|
||
dependencies { | ||
classpath 'com.android.tools.build:gradle:3.3.2' | ||
} | ||
} | ||
|
||
apply plugin: 'com.android.application' | ||
|
||
repositories { | ||
google() | ||
jcenter() | ||
} | ||
|
||
dependencies { | ||
implementation 'androidx.appcompat:appcompat:1.0.2' | ||
implementation 'com.google.android.material:material:1.0.0' | ||
implementation 'androidx.legacy:legacy-support-v13:1.0.0' | ||
implementation 'com.google.android.gms:play-services-wearable:16.0.1' | ||
|
||
|
||
implementation project(':Shared') | ||
wearApp project(':Wearable') | ||
} | ||
|
||
// The sample build uses multiple directories to | ||
// keep boilerplate and common code separate from | ||
// the main sample code. | ||
List<String> dirs = [ | ||
'main', // main sample code; look here for the interesting stuff. | ||
'common', // components that are reused by multiple samples | ||
'template'] // boilerplate code that is generated by the sample template process | ||
|
||
android { | ||
compileSdkVersion 28 | ||
|
||
buildToolsVersion "28.0.3" | ||
|
||
defaultConfig { | ||
minSdkVersion 18 | ||
targetSdkVersion 28 | ||
versionCode 1 | ||
versionName "1.0" | ||
} | ||
|
||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_7 | ||
targetCompatibility JavaVersion.VERSION_1_7 | ||
} | ||
|
||
sourceSets { | ||
main { | ||
dirs.each { dir -> | ||
java.srcDirs "src/${dir}/java" | ||
res.srcDirs "src/${dir}/res" | ||
} | ||
} | ||
androidTest.setRoot('tests') | ||
androidTest.java.srcDirs = ['tests/src'] | ||
|
||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
RuntimePermissionsWear/Application/src/main/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright 2013 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. | ||
--> | ||
|
||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.example.android.wearable.runtimepermissions" | ||
android:versionCode="1" | ||
android:versionName="1.0"> | ||
|
||
<!-- Permissions for phone. --> | ||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> | ||
|
||
<!-- Permissions for wearable: | ||
Earlier watches require their permissions to be a subset of the phone apps permission in order | ||
for the wear app to be installed. Therefore, you must include the permissions here as well as in | ||
the wear manifest. | ||
--> | ||
<uses-permission android:name="android.permission.BODY_SENSORS" /> | ||
<uses-permission android:name="android.permission.WAKE_LOCK" /> | ||
|
||
<application android:allowBackup="true" | ||
android:label="@string/app_name" | ||
android:icon="@mipmap/ic_launcher" | ||
android:theme="@style/Theme.AppCompat.Light"> | ||
<activity | ||
android:name=".MainPhoneActivity" | ||
android:label="@string/app_name" > | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
|
||
<activity | ||
android:name=".PhonePermissionRequestActivity" | ||
android:label="@string/title_activity_phone_permission_request" | ||
android:theme="@style/Theme.AppCompat.Light.NoActionBar" > | ||
</activity> | ||
|
||
<activity | ||
android:name=".WearPermissionRequestActivity" | ||
android:label="@string/title_activity_wear_permission_request" | ||
android:theme="@style/Theme.AppCompat.Light.NoActionBar" > | ||
</activity> | ||
|
||
<service | ||
android:name=".IncomingRequestPhoneService" | ||
android:enabled="true" | ||
android:exported="true" > | ||
<intent-filter> | ||
<action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" /> | ||
<data android:scheme="wear" android:host="*" android:pathPrefix="/"/> | ||
</intent-filter> | ||
</service> | ||
</application> | ||
|
||
|
||
</manifest> |
169 changes: 169 additions & 0 deletions
169
...ain/java/com/example/android/wearable/runtimepermissions/IncomingRequestPhoneService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
/* | ||
* Copyright (C) 2015 Google Inc. All Rights Reserved. | ||
* | ||
* 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.wearable.runtimepermissions; | ||
|
||
import android.Manifest; | ||
import android.content.Intent; | ||
import android.content.pm.PackageManager; | ||
import android.os.Environment; | ||
import android.util.Log; | ||
|
||
import androidx.core.app.ActivityCompat; | ||
|
||
import com.example.android.wearable.runtimepermissions.common.Constants; | ||
import com.google.android.gms.tasks.OnCompleteListener; | ||
import com.google.android.gms.tasks.Task; | ||
import com.google.android.gms.wearable.DataMap; | ||
import com.google.android.gms.wearable.MessageEvent; | ||
import com.google.android.gms.wearable.Wearable; | ||
import com.google.android.gms.wearable.WearableListenerService; | ||
|
||
import java.io.File; | ||
|
||
/** | ||
* Handles all incoming requests for phone data (and permissions) from wear devices. | ||
*/ | ||
public class IncomingRequestPhoneService extends WearableListenerService { | ||
|
||
private static final String TAG = "IncomingRequestService"; | ||
|
||
@Override | ||
public void onCreate() { | ||
super.onCreate(); | ||
Log.d(TAG, "onCreate()"); | ||
} | ||
|
||
@Override | ||
public void onMessageReceived(MessageEvent messageEvent) { | ||
super.onMessageReceived(messageEvent); | ||
Log.d(TAG, "onMessageReceived(): " + messageEvent); | ||
|
||
String messagePath = messageEvent.getPath(); | ||
|
||
if (messagePath.equals(Constants.MESSAGE_PATH_PHONE)) { | ||
|
||
DataMap dataMap = DataMap.fromByteArray(messageEvent.getData()); | ||
int requestType = dataMap.getInt(Constants.KEY_COMM_TYPE, 0); | ||
|
||
if (requestType == Constants.COMM_TYPE_REQUEST_PROMPT_PERMISSION) { | ||
promptUserForStoragePermission(messageEvent.getSourceNodeId()); | ||
|
||
} else if (requestType == Constants.COMM_TYPE_REQUEST_DATA) { | ||
respondWithStorageInformation(messageEvent.getSourceNodeId()); | ||
} | ||
} | ||
} | ||
|
||
private void promptUserForStoragePermission(String nodeId) { | ||
boolean storagePermissionApproved = | ||
ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) | ||
== PackageManager.PERMISSION_GRANTED; | ||
|
||
if (storagePermissionApproved) { | ||
DataMap dataMap = new DataMap(); | ||
dataMap.putInt(Constants.KEY_COMM_TYPE, | ||
Constants.COMM_TYPE_RESPONSE_USER_APPROVED_PERMISSION); | ||
sendMessage(nodeId, dataMap); | ||
} else { | ||
// Launch Phone Activity to grant storage permissions. | ||
Intent startIntent = new Intent(this, PhonePermissionRequestActivity.class); | ||
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | ||
|
||
/* This extra is included to alert MainPhoneActivity to send back the permission | ||
* results after the user has made their decision in PhonePermissionRequestActivity | ||
* and it finishes. | ||
*/ | ||
startIntent.putExtra(MainPhoneActivity.EXTRA_PROMPT_PERMISSION_FROM_WEAR, true); | ||
startActivity(startIntent); | ||
} | ||
} | ||
|
||
private void respondWithStorageInformation(String nodeId) { | ||
|
||
boolean storagePermissionApproved = | ||
ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) | ||
== PackageManager.PERMISSION_GRANTED; | ||
|
||
if (!storagePermissionApproved) { | ||
DataMap dataMap = new DataMap(); | ||
dataMap.putInt(Constants.KEY_COMM_TYPE, | ||
Constants.COMM_TYPE_RESPONSE_PERMISSION_REQUIRED); | ||
sendMessage(nodeId, dataMap); | ||
} else { | ||
/* To keep the sample simple, we are only displaying the top level list of directories. | ||
* Otherwise, it will return a message that the media wasn't available. | ||
*/ | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
|
||
if (isExternalStorageReadable()) { | ||
File externalStorageDirectory = Environment.getExternalStorageDirectory(); | ||
String[] fileList = externalStorageDirectory.list(); | ||
|
||
if (fileList.length > 0) { | ||
stringBuilder.append("List of directories on phone:\n"); | ||
for (String file : fileList) { | ||
stringBuilder.append(" - " + file + "\n"); | ||
} | ||
} else { | ||
stringBuilder.append("No files in external storage."); | ||
} | ||
} else { | ||
stringBuilder.append("No external media is available."); | ||
} | ||
|
||
// Send valid results | ||
DataMap dataMap = new DataMap(); | ||
dataMap.putInt(Constants.KEY_COMM_TYPE, | ||
Constants.COMM_TYPE_RESPONSE_DATA); | ||
dataMap.putString(Constants.KEY_PAYLOAD, stringBuilder.toString()); | ||
sendMessage(nodeId, dataMap); | ||
|
||
} | ||
} | ||
|
||
private void sendMessage(String nodeId, DataMap dataMap) { | ||
Log.d(TAG, "sendMessage() Node: " + nodeId); | ||
|
||
|
||
// Clients are inexpensive to create, so in this case we aren't creating member variables. | ||
// (They are cached and shared between GoogleApi instances.) | ||
Task<Integer> sendMessageTask = | ||
Wearable.getMessageClient( | ||
getApplicationContext()).sendMessage( | ||
nodeId, | ||
Constants.MESSAGE_PATH_WEAR, | ||
dataMap.toByteArray()); | ||
|
||
sendMessageTask.addOnCompleteListener(new OnCompleteListener<Integer>() { | ||
@Override | ||
public void onComplete(Task<Integer> task) { | ||
if (task.isSuccessful()) { | ||
Log.d(TAG, "Message sent successfully"); | ||
} else { | ||
Log.d(TAG, "Message failed."); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
private boolean isExternalStorageReadable() { | ||
String state = Environment.getExternalStorageState(); | ||
|
||
return Environment.MEDIA_MOUNTED.equals(state) | ||
|| Environment.MEDIA_MOUNTED_READ_ONLY.equals(state); | ||
} | ||
} |
Oops, something went wrong.