2 * Copyright (C) 2013 Koushik Dutta (@koush)
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.koushikdutta.superuser;
19 import java.util.ArrayList;
21 import android.content.Context;
22 import android.content.res.Configuration;
23 import android.graphics.drawable.Drawable;
24 import android.os.Bundle;
25 import android.support.v4.app.FragmentActivity;
26 import android.text.format.DateFormat;
27 import android.view.ContextThemeWrapper;
28 import android.view.Menu;
29 import android.view.MenuInflater;
30 import android.view.MenuItem;
31 import android.view.MenuItem.OnMenuItemClickListener;
32 import android.view.View;
33 import android.widget.ImageView;
35 import com.koushikdutta.superuser.db.SuDatabaseHelper;
36 import com.koushikdutta.superuser.db.UidPolicy;
37 import com.koushikdutta.widgets.FragmentInterfaceWrapper;
38 import com.koushikdutta.widgets.ListContentFragmentInternal;
39 import com.koushikdutta.widgets.ListItem;
41 public class PolicyFragmentInternal extends ListContentFragmentInternal {
42 public PolicyFragmentInternal(FragmentInterfaceWrapper fragment) {
46 ContextThemeWrapper mWrapper;
48 public Context getContext() {
51 mWrapper = new ContextThemeWrapper(super.getContext(), R.style.Superuser_PolicyIcon);
56 setContent(null, null);
57 getListView().clearChoices();
62 final ArrayList<UidPolicy> policies = SuDatabaseHelper.getPolicies(getActivity());
64 for (UidPolicy up: policies) {
69 FragmentInterfaceWrapper mContent;
73 public void onCreate(Bundle savedInstanceState, View view) {
74 super.onCreate(savedInstanceState, view);
76 getFragment().setHasOptionsMenu(true);
78 setEmpty(R.string.no_apps);
82 if ("com.koushikdutta.superuser".equals(getContext().getPackageName())) {
83 ImageView watermark = (ImageView)view.findViewById(R.id.watermark);
84 if (watermark != null)
85 watermark.setImageResource(R.drawable.clockwork512);
92 void addPolicy(final UidPolicy up) {
93 java.text.DateFormat df = DateFormat.getLongDateFormat(getActivity());
94 String date = df.format(up.getLastDate());
97 ListItem li = addItem(up.getPolicyResource(), new ListItem(this, up.name, date) {
98 public void onClick(View view) {
101 setContent(this, up);
105 Drawable icon = Helper.loadPackageIcon(getActivity(), up.packageName);
107 li.setIcon(R.drawable.ic_launcher);
109 li.setDrawable(icon);
112 public void onConfigurationChanged(Configuration newConfig) {
115 FragmentInterfaceWrapper setContentNative(final ListItem li, final UidPolicy up) {
116 LogNativeFragment l = new LogNativeFragment();
117 l.getInternal().setUidPolicy(up);
119 Bundle args = new Bundle();
120 args.putString("command", up.command);
121 args.putInt("uid", up.uid);
122 args.putInt("desiredUid", up.desiredUid);
123 l.setArguments(args);
125 l.getInternal().setListContentId(getFragment().getId());
129 void setContent(final ListItem li, final UidPolicy up) {
130 if (getActivity() instanceof FragmentActivity) {
131 LogFragment l = new LogFragment();
132 l.getInternal().setUidPolicy(up);
133 l.getInternal().setListContentId(getFragment().getId());
137 mContent = setContentNative(li, up);
140 setContent(mContent, up == null, up == null ? getString(R.string.logs) : up.getName());
144 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
145 super.onCreateOptionsMenu(menu, inflater);
146 MenuInflater mi = new MenuInflater(getActivity());
147 mi.inflate(R.menu.main, menu);
148 MenuItem log = menu.findItem(R.id.logs);
149 log.setOnMenuItemClickListener(new OnMenuItemClickListener() {
151 public boolean onMenuItemClick(MenuItem item) {
157 MenuItem settings = menu.findItem(R.id.settings);
158 settings.setOnMenuItemClickListener(new OnMenuItemClickListener() {
159 void openSettingsNative(final MenuItem item) {
160 setContent(new SettingsNativeFragment() {
162 public void onConfigurationChanged(Configuration newConfig) {
163 super.onConfigurationChanged(newConfig);
164 onMenuItemClick(item);
166 }, true, getString(R.string.settings));
170 public boolean onMenuItemClick(final MenuItem item) {
171 if (getActivity() instanceof FragmentActivity) {
172 setContent(new SettingsFragment() {
174 public void onConfigurationChanged(Configuration newConfig) {
175 super.onConfigurationChanged(newConfig);
176 onMenuItemClick(item);
178 }, true, getString(R.string.settings));
181 openSettingsNative(item);