OSDN Git Service

fix ndk to go to assets?
[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     int mListContentId;
53     public void setListContentId(int id) {
54         mListContentId = id;
55     }
56     
57     public int getListContentId() {
58         return mListContentId;
59     }
60     
61     void onDelete() {
62     }
63     
64     @Override
65     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
66         
67         inflater.inflate(R.menu.policy, menu);
68         MenuItem delete = menu.findItem(R.id.delete);
69         delete.setOnMenuItemClickListener(new OnMenuItemClickListener() {
70             @Override
71             public boolean onMenuItemClick(MenuItem item) {
72                 if (up != null)
73                     SuDatabaseHelper.delete(getActivity(), up);
74                 else
75                     SuDatabaseHelper.deleteLogs(getActivity());
76                 onDelete();
77                 return true;
78             }
79         });
80     }
81     
82     @Override
83     protected int getListItemResource() {
84         return R.layout.log_item;
85     }
86     
87     @Override
88     protected int getListFragmentResource() {
89         return R.layout.policy_fragment;
90     }
91     
92     @Override
93     protected void onCreate(Bundle savedInstanceState, View view) {
94         super.onCreate(savedInstanceState, view);
95         
96         getFragment().setHasOptionsMenu(true);
97         
98         if (up == null) {
99             Bundle bundle = getFragment().getArguments();
100             if (bundle != null) {
101                 String command = bundle.getString("command");
102                 int uid = bundle.getInt("uid", -1);
103                 int desiredUid = bundle.getInt("desiredUid", -1);
104                 if (uid != -1 && desiredUid != -1) {
105                     up = SuDatabaseHelper.get(getContext(), uid, desiredUid, command);
106                 }
107             }
108         }
109         
110         ArrayList<LogEntry> logs;
111         java.text.DateFormat day = DateFormat.getDateFormat(getActivity());
112         java.text.DateFormat time = DateFormat.getTimeFormat(getActivity());
113         if (up != null) {
114             ImageView icon = (ImageView)view.findViewById(R.id.image);
115             icon.setImageDrawable(Helper.loadPackageIcon(getActivity(), up.packageName));
116             TextView name = (TextView)view.findViewById(R.id.name);
117             name.setText(up.name);
118             
119             ((TextView)view.findViewById(R.id.uid_header)).setText(Integer.toString(up.desiredUid));
120             ((TextView)view.findViewById(R.id.command_header)).setText(up.command == null ? getString(R.string.all_commands) : up.command);
121             String app = up.username;
122             if (app == null || app.length() == 0)
123                 app = String.valueOf(up.uid);
124             ((TextView)view.findViewById(R.id.app_header)).setText(app);
125             ((TextView)view.findViewById(R.id.package_header)).setText(up.packageName);
126
127             getListView().setSelector(android.R.color.transparent);
128
129             logs = SuDatabaseHelper.getLogs(getActivity(), up, -1);
130         }
131         else {
132             view.findViewById(R.id.title_container).setVisibility(View.GONE);
133             logs = SuDatabaseHelper.getLogs(getActivity());
134         }
135         
136         setEmpty(R.string.no_logs);
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 cb = (CompoundButton)view.findViewById(R.id.logging);
158         cb.setOnClickListener(new OnClickListener() {
159             @Override
160             public void onClick(View v) {
161                 if (up == null) {
162                     Settings.setLogging(getActivity(), cb.isChecked());
163                 }
164                 else {
165                     up.logging = cb.isChecked();
166                     SuDatabaseHelper.setPolicy(getActivity(), up);
167                 }
168             }
169         });
170         if (up == null) {
171             cb.setChecked(Settings.getLogging(getActivity()));
172         }
173         else {
174             cb.setChecked(up.logging);
175         }
176     }
177 }