OSDN Git Service

Add MIPS support
[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.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;
34
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;
43
44 public class LogFragmentInternal extends BetterListFragmentInternal {
45     public LogFragmentInternal(FragmentInterfaceWrapper fragment) {
46         super(fragment);
47     }
48
49     UidPolicy up;
50     public LogFragmentInternal setUidPolicy(UidPolicy up) {
51         this.up = up;
52         return this;
53     }
54     
55     int mListContentId;
56     public void setListContentId(int id) {
57         mListContentId = id;
58     }
59     
60     public int getListContentId() {
61         return mListContentId;
62     }
63     
64     void onDelete() {
65     }
66     
67     @Override
68     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
69         
70         inflater.inflate(R.menu.policy, menu);
71         MenuItem delete = menu.findItem(R.id.delete);
72         delete.setOnMenuItemClickListener(new OnMenuItemClickListener() {
73             @Override
74             public boolean onMenuItemClick(MenuItem item) {
75                 if (up != null)
76                     SuDatabaseHelper.delete(getActivity(), up);
77                 else
78                     SuperuserDatabaseHelper.deleteLogs(getActivity());
79                 onDelete();
80                 return true;
81             }
82         });
83     }
84     
85     @Override
86     protected int getListItemResource() {
87         return R.layout.log_item;
88     }
89
90     @Override
91     protected void onCreate(Bundle savedInstanceState, View view) {
92         super.onCreate(savedInstanceState, view);
93         
94         LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
95         getListView().addHeaderView(inflater.inflate(R.layout.policy_header, null));
96         
97         getFragment().setHasOptionsMenu(true);
98         
99         if (up == null) {
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);
107                 }
108             }
109         }
110         
111         ArrayList<LogEntry> logs;
112         java.text.DateFormat day = DateFormat.getDateFormat(getActivity());
113         java.text.DateFormat time = DateFormat.getTimeFormat(getActivity());
114         if (up != null) {
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);
119             
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);
127
128             getListView().setSelector(android.R.color.transparent);
129
130             logs = SuperuserDatabaseHelper.getLogs(getActivity(), up, -1);
131         }
132         else {
133             setEmpty(R.string.no_logs);
134             view.findViewById(R.id.policy_header).setVisibility(View.GONE);
135             logs = SuperuserDatabaseHelper.getLogs(getActivity());
136         }
137         
138         for (LogEntry log: logs) {
139             final String date = time.format(log.getDate());
140             String title = date;
141             String summary = getString(log.getActionResource());
142             if (up == null) {
143                 title = log.getName();
144             }
145             addItem(day.format(log.getDate()), new ListItem(this, title, summary, null) {
146                 @Override
147                 public View getView(android.content.Context context, View convertView) {
148                     View ret = super.getView(context, convertView);
149                     if (up == null) {
150                         ((TextView)ret.findViewById(R.id.extra)).setText(date);
151                     }
152                     return ret;
153                 }
154             });
155         }
156
157         final CompoundButton logging = (CompoundButton)view.findViewById(R.id.logging);
158         if (up == null) {
159             logging.setChecked(Settings.getLogging(getActivity()));
160         }
161         else {
162             logging.setChecked(up.logging);
163         }
164         logging.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
165             @Override
166             public void onCheckedChanged(CompoundButton button, boolean isChecked) {
167                 if (up == null) {
168                     Settings.setLogging(getActivity(), isChecked);
169                 }
170                 else {
171                     up.logging = isChecked;
172                     SuDatabaseHelper.setPolicy(getActivity(), up);
173                 }
174             }
175         });
176
177         final CompoundButton notification = (CompoundButton)view.findViewById(R.id.notification);
178         if (up == null) {
179             view.findViewById(R.id.notification_container).setVisibility(View.GONE);
180         }
181         else {
182             notification.setChecked(up.notification);
183         }
184         notification.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
185             @Override
186             public void onCheckedChanged(CompoundButton button, boolean isChecked) {
187                 if (up == null) {
188                 }
189                 else {
190                     up.notification = isChecked;
191                     SuDatabaseHelper.setPolicy(getActivity(), up);
192                 }
193             }
194         });
195     }
196 }