165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/*
265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Copyright (C) 2014 The Android Open Source Project
365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Licensed under the Apache License, Version 2.0 (the "License");
565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * you may not use this file except in compliance with the License.
665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * You may obtain a copy of the License at
765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *      http://www.apache.org/licenses/LICENSE-2.0
965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
1065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Unless required by applicable law or agreed to in writing, software
1165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * distributed under the License is distributed on an "AS IS" BASIS,
1265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * See the License for the specific language governing permissions and
1465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * limitations under the License.
1565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
1665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
1765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepackage com.android.tv.settings.device.apps;
1865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
1965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport com.android.tv.settings.R;
2065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
2165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.app.Activity;
2265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.app.ActivityManager;
2365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.Context;
2465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.Intent;
2565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.pm.IPackageDataObserver;
2665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
2765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/**
2865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Handles clearing an application's data.
2965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
3065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneclass DataClearer {
3165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
3265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    interface Listener {
3365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        void dataCleared(boolean succeeded);
3465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
3565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
3665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private final Listener mListener;
3765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private final AppInfo mAppInfo;
3865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private boolean mClearingData;
3965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
4065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    DataClearer(Listener listener, AppInfo appInfo) {
4165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mListener = listener;
4265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mAppInfo = appInfo;
4365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mClearingData = false;
4465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
4565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
4665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    void onActivityResult(int resultCode) {
4765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mClearingData = false;
4865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (resultCode == Activity.RESULT_OK) {
4965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mListener.dataCleared(true);
5065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else {
5165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mListener.dataCleared(false);
5265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
5365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
5465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
5565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    void clearData(Activity activity, int requestId) {
5665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mClearingData = true;
5765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        String spaceManagementActivityName = mAppInfo.getSpaceManagerActivityName();
5865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (spaceManagementActivityName != null) {
5965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (!ActivityManager.isUserAMonkey()) {
6065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                Intent intent = new Intent(Intent.ACTION_DEFAULT);
6165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                intent.setClassName(mAppInfo.getPackageName(), spaceManagementActivityName);
6265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                activity.startActivityForResult(intent, requestId);
6365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
6465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else {
6565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            ActivityManager am = (ActivityManager) activity.getSystemService(
6665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    Context.ACTIVITY_SERVICE);
6765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            boolean res = am.clearApplicationUserData(
6865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    mAppInfo.getPackageName(), new IPackageDataObserver.Stub() {
6965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        public void onRemoveCompleted(
7065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                                final String mPackageName, final boolean succeeded) {
7165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                            mClearingData = false;
7265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                            if (succeeded) {
7365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                                mListener.dataCleared(true);
7465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                            } else {
7565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                                mListener.dataCleared(false);
7665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                            }
7765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        }
7865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    });
7965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (!res) {
8065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mClearingData = false;
8165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mListener.dataCleared(false);
8265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
8365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
8465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
8565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        // Send out broadcast to clear corresponding app's canvas disk cache.
8665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        Intent intent = new Intent();
8765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        intent.setAction("com.google.android.canvas.data.ClusterDiskCache.CLEAR_CACHE_APP");
8865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        intent.putExtra("packageName", mAppInfo.getPackageName());
8965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        activity.sendBroadcast(intent);
9065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
9165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
9265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    String getDataSize(Context context) {
9365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return (mClearingData) ? context.getString(R.string.computing_size)
9465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                : mAppInfo.getDataSize();
9565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
9665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane}
97