From d704ae25525b27e2c03937ad35a32c05e58a4b3d Mon Sep 17 00:00:00 2001 From: Fyodor Kupolov Date: Mon, 13 Mar 2017 16:39:27 -0700 Subject: [PATCH] APK Cache Demo App Simple app that lists files in the app's preloaded cache directory. (/data/preloads/file_cache/com.android.apkcachetest) Test: make ApkCacheTest Bug: 38434471 Change-Id: Ia98650e41bc383d45ebf620bc065deb68076a75f --- samples/apkcachetest/Android.mk | 30 ++++++++ samples/apkcachetest/AndroidManifest.xml | 31 +++++++++ .../res/layout/hello_activity.xml | 20 ++++++ .../src/com/android/apkcachetest/Main.java | 69 +++++++++++++++++++ 4 files changed, 150 insertions(+) create mode 100644 samples/apkcachetest/Android.mk create mode 100644 samples/apkcachetest/AndroidManifest.xml create mode 100644 samples/apkcachetest/res/layout/hello_activity.xml create mode 100644 samples/apkcachetest/src/com/android/apkcachetest/Main.java diff --git a/samples/apkcachetest/Android.mk b/samples/apkcachetest/Android.mk new file mode 100644 index 000000000..d7c7a444f --- /dev/null +++ b/samples/apkcachetest/Android.mk @@ -0,0 +1,30 @@ +# +# Copyright (C) 2016 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. +# + +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_MODULE_TAGS := tests + +LOCAL_SRC_FILES := $(call all-java-files-under, src) + +LOCAL_PACKAGE_NAME := ApkCacheTest + +LOCAL_CERTIFICATE := shared + +LOCAL_PROGUARD_ENABLED := disabled + +include $(BUILD_PACKAGE) diff --git a/samples/apkcachetest/AndroidManifest.xml b/samples/apkcachetest/AndroidManifest.xml new file mode 100644 index 000000000..de8ac01a2 --- /dev/null +++ b/samples/apkcachetest/AndroidManifest.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + diff --git a/samples/apkcachetest/res/layout/hello_activity.xml b/samples/apkcachetest/res/layout/hello_activity.xml new file mode 100644 index 000000000..ae33e784c --- /dev/null +++ b/samples/apkcachetest/res/layout/hello_activity.xml @@ -0,0 +1,20 @@ + + + + \ No newline at end of file diff --git a/samples/apkcachetest/src/com/android/apkcachetest/Main.java b/samples/apkcachetest/src/com/android/apkcachetest/Main.java new file mode 100644 index 000000000..7fb45575b --- /dev/null +++ b/samples/apkcachetest/src/com/android/apkcachetest/Main.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2017 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 + */ + +package com.android.apkcachetest; + +import android.app.Activity; +import android.os.Bundle; +import android.os.FileUtils; +import android.widget.TextView; + +import java.io.File; +import java.io.IOException; + +public class Main extends Activity { + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setContentView(R.layout.hello_activity); + File preloadsFileCache = getPreloadsFileCache(); + TextView txt = (TextView) findViewById(R.id.text); + if (!getApplicationInfo().isPrivilegedApp()) { + txt.append("WARNING: App must be installed in /system/priv-app directory to access " + + "preloads cache\n"); + } + txt.append("PreloadsFileCache app directory: " + preloadsFileCache + '\n'); + if (!preloadsFileCache.exists()) { + txt.append(" --- Directory does not exist ---\n"); + } else { + File[] files = preloadsFileCache.listFiles(); + if (files == null || files.length == 0) { + txt.append(" --- No files found ---\n"); + } else { + for (File file : files) { + try { + txt.append(" " + file.getName() + ": [" + readTextFile(file) + "]\n"); + } catch (IOException e) { + txt.append(" " + file.getName() + ": Error " + e + "\n"); + e.printStackTrace(); + } + } + txt.append(files.length + " files"); + } + + } + } + + String readTextFile(File file) throws IOException { + String s = FileUtils.readTextFile(file, 100, "..."); + if (s != null) { + s = s.replaceAll("[^\\x20-\\x7E]+", " "); + } + return s; + } +}