1 /*
2  * Copyright (C) 2017 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.apkcachetest;
18 
19 import android.app.Activity;
20 import android.os.Bundle;
21 import android.os.FileUtils;
22 import android.widget.TextView;
23 
24 import java.io.File;
25 import java.io.IOException;
26 
27 public class Main extends Activity {
28 
29     @Override
onCreate(Bundle savedInstanceState)30     public void onCreate(Bundle savedInstanceState) {
31         super.onCreate(savedInstanceState);
32 
33         setContentView(R.layout.hello_activity);
34         File preloadsFileCache = getPreloadsFileCache();
35         TextView txt = (TextView) findViewById(R.id.text);
36         if (!getApplicationInfo().isPrivilegedApp()) {
37             txt.append("WARNING: App must be installed in /system/priv-app directory to access "
38                     + "preloads cache\n");
39         }
40         txt.append("PreloadsFileCache app directory: " + preloadsFileCache + '\n');
41         if (!preloadsFileCache.exists()) {
42             txt.append("   --- Directory does not exist ---\n");
43         } else {
44             File[] files = preloadsFileCache.listFiles();
45             if (files == null || files.length == 0) {
46                 txt.append("   --- No files found ---\n");
47             } else {
48                 for (File file : files) {
49                     try {
50                         txt.append("   " + file.getName() + ": [" + readTextFile(file) + "]\n");
51                     } catch (IOException e) {
52                         txt.append("   " + file.getName() + ": Error " + e + "\n");
53                         e.printStackTrace();
54                     }
55                 }
56                 txt.append(files.length + " files");
57             }
58 
59         }
60     }
61 
readTextFile(File file)62     String readTextFile(File file) throws IOException {
63         String s = FileUtils.readTextFile(file, 100, "...");
64         if (s != null) {
65             s = s.replaceAll("[^\\x20-\\x7E]+", " ");
66         }
67         return s;
68     }
69 }
70