1 /*
2  * Copyright (C) 2022 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 example.vib;
18 
19 import android.app.Activity;
20 import android.hardware.vibrator.IVibrator;
21 import android.os.Bundle;
22 import android.os.IBinder;
23 import android.os.RemoteException;
24 
25 public class MyActivity extends Activity {
gimme(String name)26     private static native IBinder gimme(String name);
27 
28     @Override
onCreate(Bundle b)29     public void onCreate(Bundle b) {
30         super.onCreate(b);
31         System.loadLibrary("example_vib_getter");
32 
33         // There is no API to get ahold of a Stable AIDL service from a vendor app
34         // in Java. This is because this is not the recommended way to get ahold
35         // of functionality in Android. The Android API Council recommendation is to
36         // implement uses-library APIs in the system/system_ext partition which add
37         // new APIs. AIDL as an API in Java is not recommended or supported way to
38         // communicate by apps - the recommendation is to use Java APIs. However,
39         // there also exists a large number of vendor apps which are coupled with
40         // hardware-specific code, and are therefore on the vendor partition. A
41         // large number of these use HIDL, and this is how they can continue to
42         // use that structure with AIDL.
43         IVibrator v =
44                 IVibrator.Stub.asInterface(gimme("android.hardware.vibrator.IVibrator/default"));
45 
46         try {
47             v.on(100 /*ms*/, null /*cb*/);
48         } catch (RemoteException e) {
49             throw new RuntimeException(e);
50         }
51 
52         finish();
53     }
54 }
55