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.os.Bundle;
23 import android.text.TextUtils;
24 import android.text.format.DateFormat;
25 import android.view.LayoutInflater;
26 import android.view.Menu;
27 import android.view.MenuInflater;
28 import android.view.MenuItem;
29 import android.view.MenuItem.OnMenuItemClickListener;
30 import android.view.View;
31 import android.widget.CompoundButton;
32 import android.widget.ImageView;
33 import android.widget.TextView;
35 import com.koushikdutta.superuser.db.LogEntry;
36 import com.koushikdutta.superuser.db.SuDatabaseHelper;
37 import com.koushikdutta.superuser.db.SuperuserDatabaseHelper;
38 import com.koushikdutta.superuser.db.UidPolicy;
39 import com.koushikdutta.superuser.util.Settings;
40 import com.koushikdutta.widgets.BetterListFragmentInternal;
41 import com.koushikdutta.widgets.FragmentInterfaceWrapper;
42 import com.koushikdutta.widgets.ListItem;
44 public class LogFragmentInternal extends BetterListFragmentInternal {
45 public LogFragmentInternal(FragmentInterfaceWrapper fragment) {
50 public LogFragmentInternal setUidPolicy(UidPolicy up) {
56 public void setListContentId(int id) {
60 public int getListContentId() {
61 return mListContentId;
68 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
70 inflater.inflate(R.menu.policy, menu);
71 MenuItem delete = menu.findItem(R.id.delete);
72 delete.setOnMenuItemClickListener(new OnMenuItemClickListener() {
74 public boolean onMenuItemClick(MenuItem item) {
76 SuDatabaseHelper.delete(getActivity(), up);
78 SuperuserDatabaseHelper.deleteLogs(getActivity());
86 protected int getListItemResource() {
87 return R.layout.log_item;
91 protected void onCreate(Bundle savedInstanceState, View view) {
92 super.onCreate(savedInstanceState, view);
94 LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
95 getListView().addHeaderView(inflater.inflate(R.layout.policy_header, null));
97 getFragment().setHasOptionsMenu(true);
100 Bundle bundle = getFragment().getArguments();
101 if (bundle != null) {
102 String command = bundle.getString("command");
103 int uid = bundle.getInt("uid", -1);
104 int desiredUid = bundle.getInt("desiredUid", -1);
105 if (uid != -1 && desiredUid != -1) {
106 up = SuDatabaseHelper.get(getContext(), uid, desiredUid, command);
111 ArrayList<LogEntry> logs;
112 java.text.DateFormat day = DateFormat.getDateFormat(getActivity());
113 java.text.DateFormat time = DateFormat.getTimeFormat(getActivity());
115 ImageView icon = (ImageView)view.findViewById(R.id.image);
116 icon.setImageDrawable(Helper.loadPackageIcon(getActivity(), up.packageName));
117 TextView name = (TextView)view.findViewById(R.id.name);
118 name.setText(up.name);
120 ((TextView)view.findViewById(R.id.uid_header)).setText(Integer.toString(up.desiredUid));
121 ((TextView)view.findViewById(R.id.command_header)).setText(TextUtils.isEmpty(up.command) ? getString(R.string.all_commands) : up.command);
122 String app = up.username;
123 if (app == null || app.length() == 0)
124 app = String.valueOf(up.uid);
125 ((TextView)view.findViewById(R.id.app_header)).setText(app);
126 ((TextView)view.findViewById(R.id.package_header)).setText(up.packageName);
128 getListView().setSelector(android.R.color.transparent);
130 logs = SuperuserDatabaseHelper.getLogs(getActivity(), up, -1);
133 setEmpty(R.string.no_logs);
134 view.findViewById(R.id.policy_header).setVisibility(View.GONE);
135 logs = SuperuserDatabaseHelper.getLogs(getActivity());
138 for (LogEntry log: logs) {
139 final String date = time.format(log.getDate());
141 String summary = getString(log.getActionResource());
143 title = log.getName();
145 addItem(day.format(log.getDate()), new ListItem(this, title, summary, null) {
147 public View getView(android.content.Context context, View convertView) {
148 View ret = super.getView(context, convertView);
150 ((TextView)ret.findViewById(R.id.extra)).setText(date);
157 final CompoundButton logging = (CompoundButton)view.findViewById(R.id.logging);
159 logging.setChecked(Settings.getLogging(getActivity()));
162 logging.setChecked(up.logging);
164 logging.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
166 public void onCheckedChanged(CompoundButton button, boolean isChecked) {
168 Settings.setLogging(getActivity(), isChecked);
171 up.logging = isChecked;
172 SuDatabaseHelper.setPolicy(getActivity(), up);
177 final CompoundButton notification = (CompoundButton)view.findViewById(R.id.notification);
179 view.findViewById(R.id.notification_container).setVisibility(View.GONE);
182 notification.setChecked(up.notification);
184 notification.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
186 public void onCheckedChanged(CompoundButton button, boolean isChecked) {
190 up.notification = isChecked;
191 SuDatabaseHelper.setPolicy(getActivity(), up);