OSDN Git Service

LICENSE
[android-x86/external-koush-Superuser.git] / Superuser / src / com / koushikdutta / superuser / LogFragmentInternal.java
1 /*
2  * Copyright (C) 2013 Koushik Dutta (@koush)
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.koushikdutta.superuser;
18
19 import java.util.ArrayList;
20
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;
32
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;
40
41 public class LogFragmentInternal extends BetterListFragmentInternal {
42     public LogFragmentInternal(FragmentInterfaceWrapper fragment) {
43         super(fragment);
44     }
45
46     UidPolicy up;
47     public LogFragmentInternal setUidPolicy(UidPolicy up) {
48         this.up = up;
49         return this;
50     }
51     
52     void onDelete() {
53     }
54     
55     @Override
56     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
57         
58         inflater.inflate(R.menu.policy, menu);
59         MenuItem delete = menu.findItem(R.id.delete);
60         delete.setOnMenuItemClickListener(new OnMenuItemClickListener() {
61             @Override
62             public boolean onMenuItemClick(MenuItem item) {
63                 if (up != null)
64                     SuDatabaseHelper.delete(getActivity(), up);
65                 else
66                     SuDatabaseHelper.deleteLogs(getActivity());
67                 onDelete();
68                 return true;
69             }
70         });
71     }
72     
73     @Override
74     protected int getListItemResource() {
75         return R.layout.log_item;
76     }
77     
78     @Override
79     protected int getListFragmentResource() {
80         return R.layout.policy_fragment;
81     }
82     
83     @Override
84     protected void onCreate(Bundle savedInstanceState, View view) {
85         super.onCreate(savedInstanceState, view);
86         
87         getFragment().setHasOptionsMenu(true);
88         
89         if (up == null) {
90             Bundle bundle = getFragment().getArguments();
91             if (bundle != null) {
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);
97                 }
98             }
99         }
100         
101         ArrayList<LogEntry> logs;
102         java.text.DateFormat day = DateFormat.getDateFormat(getActivity());
103         java.text.DateFormat time = DateFormat.getTimeFormat(getActivity());
104         if (up != null) {
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);
109             
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);
117
118             getListView().setSelector(android.R.color.transparent);
119
120             logs = SuDatabaseHelper.getLogs(getActivity(), up, -1);
121         }
122         else {
123             view.findViewById(R.id.title_container).setVisibility(View.GONE);
124             logs = SuDatabaseHelper.getLogs(getActivity());
125         }
126         
127         setEmpty(R.string.no_logs);
128         
129         for (LogEntry log: logs) {
130             final String date = time.format(log.getDate());
131             String title = date;
132             String summary = getString(log.getActionResource());
133             if (up == null) {
134                 title = log.getName();
135             }
136             addItem(day.format(log.getDate()), new ListItem(this, title, summary, null) {
137                 @Override
138                 public View getView(android.content.Context context, View convertView) {
139                     View ret = super.getView(context, convertView);
140                     if (up == null) {
141                         ((TextView)ret.findViewById(R.id.extra)).setText(date);
142                     }
143                     return ret;
144                 }
145             });
146         }
147
148         final CompoundButton cb = (CompoundButton)view.findViewById(R.id.logging);
149         cb.setOnClickListener(new OnClickListener() {
150             @Override
151             public void onClick(View v) {
152                 if (up == null) {
153                     Settings.setLogging(getActivity(), cb.isChecked());
154                 }
155                 else {
156                     up.logging = cb.isChecked();
157                     SuDatabaseHelper.setPolicy(getActivity(), up);
158                 }
159             }
160         });
161         if (up == null) {
162             cb.setChecked(Settings.getLogging(getActivity()));
163         }
164         else {
165             cb.setChecked(up.logging);
166         }
167     }
168 }