Home | History | Annotate | Download | only in splitapp
      1 /*
      2  * Copyright (C) 2016 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 com.android.cts.splitapp;
     18 
     19 import android.content.BroadcastReceiver;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.provider.Settings;
     23 import android.util.Log;
     24 
     25 import java.io.File;
     26 
     27 public class BaseBootReceiver extends BroadcastReceiver {
     28     private static final String TAG = "SplitApp";
     29 
     30     @Override
     31     public void onReceive(Context context, Intent intent) {
     32         try {
     33             context = context.createDeviceProtectedStorageContext();
     34             final File probe = new File(context.getFilesDir(),
     35                     getBootCount(context) + "." + intent.getAction());
     36             Log.d(TAG, "Touching probe " + probe);
     37             probe.createNewFile();
     38             exposeFile(probe);
     39         } catch (Exception e) {
     40             throw new RuntimeException(e);
     41         }
     42     }
     43 
     44     private static int getBootCount(Context context) throws Exception {
     45         return Settings.Global.getInt(context.getContentResolver(), Settings.Global.BOOT_COUNT);
     46     }
     47 
     48     private static File exposeFile(File file) throws Exception {
     49         file.setReadable(true, false);
     50         file.setReadable(true, true);
     51 
     52         File dir = file.getParentFile();
     53         do {
     54             dir.setExecutable(true, false);
     55             dir.setExecutable(true, true);
     56             dir = dir.getParentFile();
     57         } while (dir != null);
     58 
     59         return file;
     60     }
     61 }
     62