Home | History | Annotate | Download | only in activity
      1 /*
      2  * Copyright 2013 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 android.security.cts.activity;
     18 
     19 import android.os.Handler;
     20 import android.os.IBinder;
     21 import android.os.Message;
     22 import android.security.cts.activity.ISecureRandomService;
     23 
     24 import android.app.Service;
     25 import android.content.Intent;
     26 
     27 import java.security.SecureRandom;
     28 
     29 public class SecureRandomService extends Service {
     30     /**
     31      * This helps the process shut down a little faster and get us a new
     32      * PID earlier than calling stopService.
     33      */
     34     private Handler mShutdownHandler = new Handler() {
     35         @Override
     36         public void handleMessage(Message msg) {
     37             stopSelf();
     38         }
     39     };
     40 
     41     private final ISecureRandomService.Stub mBinder = new ISecureRandomService.Stub() {
     42 
     43         /**
     44          * Returns output from SecureRandom and the current process PID. Note
     45          * that this should only be called once. To ensure that it's only called
     46          * once, this will throw an error if it's called twice in a row.
     47          */
     48         public int getRandomBytesAndPid(byte[] randomBytes) {
     49             mShutdownHandler.sendEmptyMessage(-1);
     50 
     51             SecureRandom sr = new SecureRandom();
     52             sr.nextBytes(randomBytes);
     53             return android.os.Process.myPid();
     54         }
     55     };
     56 
     57     @Override
     58     public IBinder onBind(Intent intent) {
     59         return mBinder;
     60     }
     61 }
     62