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.os.Bundle;
22 import android.text.format.DateFormat;
23 import android.view.Menu;
24 import android.view.MenuInflater;
25 import android.view.MenuItem;
26 import android.view.MenuItem.OnMenuItemClickListener;
27 import android.view.View;
28 import android.view.View.OnClickListener;
29 import android.widget.CompoundButton;
30 import android.widget.ImageView;
31 import android.widget.TextView;
33 import com.koushikdutta.superuser.db.LogEntry;
34 import com.koushikdutta.superuser.db.SuDatabaseHelper;
35 import com.koushikdutta.superuser.db.UidPolicy;
36 import com.koushikdutta.superuser.util.Settings;
37 import com.koushikdutta.widgets.BetterListFragmentInternal;
38 import com.koushikdutta.widgets.FragmentInterfaceWrapper;
39 import com.koushikdutta.widgets.ListItem;
41 public class LogFragmentInternal extends BetterListFragmentInternal {
42 public LogFragmentInternal(FragmentInterfaceWrapper fragment) {
47 public LogFragmentInternal setUidPolicy(UidPolicy up) {
56 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
58 inflater.inflate(R.menu.policy, menu);
59 MenuItem delete = menu.findItem(R.id.delete);
60 delete.setOnMenuItemClickListener(new OnMenuItemClickListener() {
62 public boolean onMenuItemClick(MenuItem item) {
64 SuDatabaseHelper.delete(getActivity(), up);
66 SuDatabaseHelper.deleteLogs(getActivity());
74 protected int getListItemResource() {
75 return R.layout.log_item;
79 protected int getListFragmentResource() {
80 return R.layout.policy_fragment;
84 protected void onCreate(Bundle savedInstanceState, View view) {
85 super.onCreate(savedInstanceState, view);
87 getFragment().setHasOptionsMenu(true);
90 Bundle bundle = getFragment().getArguments();
92 String command = bundle.getString("command");
93 int uid = bundle.getInt("uid", -1);
94 int desiredUid = bundle.getInt("desiredUid", -1);
95 if (uid != -1 && desiredUid != -1) {
96 up = SuDatabaseHelper.get(getContext(), uid, desiredUid, command);
101 ArrayList<LogEntry> logs;
102 java.text.DateFormat day = DateFormat.getDateFormat(getActivity());
103 java.text.DateFormat time = DateFormat.getTimeFormat(getActivity());
105 ImageView icon = (ImageView)view.findViewById(R.id.image);
106 icon.setImageDrawable(Helper.loadPackageIcon(getActivity(), up.packageName));
107 TextView name = (TextView)view.findViewById(R.id.name);
108 name.setText(up.name);
110 ((TextView)view.findViewById(R.id.uid_header)).setText(Integer.toString(up.desiredUid));
111 ((TextView)view.findViewById(R.id.command_header)).setText(up.command == null ? getString(R.string.all_commands) : up.command);
112 String app = up.username;
113 if (app == null || app.length() == 0)
114 app = String.valueOf(up.uid);
115 ((TextView)view.findViewById(R.id.app_header)).setText(app);
116 ((TextView)view.findViewById(R.id.package_header)).setText(up.packageName);
118 getListView().setSelector(android.R.color.transparent);
120 logs = SuDatabaseHelper.getLogs(getActivity(), up, -1);
123 view.findViewById(R.id.title_container).setVisibility(View.GONE);
124 logs = SuDatabaseHelper.getLogs(getActivity());
127 setEmpty(R.string.no_logs);
129 for (LogEntry log: logs) {
130 final String date = time.format(log.getDate());
132 String summary = getString(log.getActionResource());
134 title = log.getName();
136 addItem(day.format(log.getDate()), new ListItem(this, title, summary, null) {
138 public View getView(android.content.Context context, View convertView) {
139 View ret = super.getView(context, convertView);
141 ((TextView)ret.findViewById(R.id.extra)).setText(date);
148 final CompoundButton cb = (CompoundButton)view.findViewById(R.id.logging);
149 cb.setOnClickListener(new OnClickListener() {
151 public void onClick(View v) {
153 Settings.setLogging(getActivity(), cb.isChecked());
156 up.logging = cb.isChecked();
157 SuDatabaseHelper.setPolicy(getActivity(), up);
162 cb.setChecked(Settings.getLogging(getActivity()));
165 cb.setChecked(up.logging);