OSDN Git Service

fix up some db churn by reusing a single db connection
[android-x86/external-koush-Superuser.git] / Superuser / src / com / koushikdutta / superuser / PolicyFragmentInternal.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 import java.util.Date;
21
22 import android.content.Context;
23 import android.content.res.Configuration;
24 import android.database.sqlite.SQLiteDatabase;
25 import android.graphics.drawable.Drawable;
26 import android.os.Bundle;
27 import android.support.v4.app.FragmentActivity;
28 import android.text.format.DateFormat;
29 import android.view.ContextThemeWrapper;
30 import android.view.Menu;
31 import android.view.MenuInflater;
32 import android.view.MenuItem;
33 import android.view.MenuItem.OnMenuItemClickListener;
34 import android.view.View;
35 import android.widget.ImageView;
36
37 import com.koushikdutta.superuser.db.LogEntry;
38 import com.koushikdutta.superuser.db.SuDatabaseHelper;
39 import com.koushikdutta.superuser.db.SuperuserDatabaseHelper;
40 import com.koushikdutta.superuser.db.UidPolicy;
41 import com.koushikdutta.widgets.FragmentInterfaceWrapper;
42 import com.koushikdutta.widgets.ListContentFragmentInternal;
43 import com.koushikdutta.widgets.ListItem;
44
45 public class PolicyFragmentInternal extends ListContentFragmentInternal {
46     public PolicyFragmentInternal(FragmentInterfaceWrapper fragment) {
47         super(fragment);
48     }
49     
50     ContextThemeWrapper mWrapper;
51     @Override
52     public Context getContext() {
53         if (mWrapper != null)
54             return mWrapper;
55         mWrapper = new ContextThemeWrapper(super.getContext(), R.style.Superuser_PolicyIcon);
56         return mWrapper;
57     }
58     
59     void showAllLogs() {
60         setContent(null, null);
61         getListView().clearChoices();
62     }
63     
64     void load() {
65         clear();
66         final ArrayList<UidPolicy> policies = SuDatabaseHelper.getPolicies(getActivity());
67         
68         SQLiteDatabase db = new SuperuserDatabaseHelper(getActivity()).getReadableDatabase(); 
69         try {
70             for (UidPolicy up: policies) {
71                 int last = 0;
72                 ArrayList<LogEntry> logs = SuperuserDatabaseHelper.getLogs(db, up, 1);
73                 if (logs.size() > 0)
74                     last = logs.get(0).date;
75                 addPolicy(up, last);
76             }
77         }
78         finally {
79             db.close();
80         }
81     }
82     
83     public void onResume() {
84         super.onResume();
85         load();
86     }
87
88     FragmentInterfaceWrapper mContent;
89
90     
91     @Override
92     public void onCreate(Bundle savedInstanceState, View view) {
93         super.onCreate(savedInstanceState, view);
94
95         getFragment().setHasOptionsMenu(true);
96         
97         setEmpty(R.string.no_apps);
98         
99         load();
100
101         if ("com.koushikdutta.superuser".equals(getContext().getPackageName())) {
102             ImageView watermark = (ImageView)view.findViewById(R.id.watermark);
103             if (watermark != null)
104                 watermark.setImageResource(R.drawable.clockwork512);
105         }
106         if (!isPaged())
107             showAllLogs();
108     }
109     
110     public Date getLastDate(int last) {
111         return new Date((long)last * 1000);
112     }
113
114     void addPolicy(final UidPolicy up, final int last) {
115         java.text.DateFormat df = DateFormat.getLongDateFormat(getActivity());
116         String date;
117         if (last == 0)
118             date = null;
119         else
120             date = df.format(getLastDate(last));
121         ListItem li = addItem(up.getPolicyResource(), new ListItem(this, up.name, date) {
122             public void onClick(View view) {
123                 super.onClick(view);
124
125                 setContent(this, up);
126             };
127         });
128         
129         Drawable icon = Helper.loadPackageIcon(getActivity(), up.packageName);
130         if (icon == null)
131             li.setIcon(R.drawable.ic_launcher);
132         else
133             li.setDrawable(icon);
134     }
135     
136     public void onConfigurationChanged(Configuration newConfig) {
137     };
138
139     FragmentInterfaceWrapper setContentNative(final ListItem li, final UidPolicy up) {
140         LogNativeFragment l = new LogNativeFragment();
141         l.getInternal().setUidPolicy(up);
142         if (up != null) {
143             Bundle args = new Bundle();
144             args.putString("command", up.command);
145             args.putInt("uid", up.uid);
146             args.putInt("desiredUid", up.desiredUid);
147             l.setArguments(args);
148         }
149         l.getInternal().setListContentId(getFragment().getId());
150         return l;
151     }
152     
153     void setContent(final ListItem li, final UidPolicy up) {
154         if (getActivity() instanceof FragmentActivity) {
155             LogFragment l = new LogFragment();
156             l.getInternal().setUidPolicy(up);
157             l.getInternal().setListContentId(getFragment().getId());
158             mContent = l;
159         }
160         else {
161             mContent = setContentNative(li, up);
162         }
163         
164         setContent(mContent, up == null, up == null ? getString(R.string.logs) : up.getName());
165     }
166     
167     @Override
168     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
169         super.onCreateOptionsMenu(menu, inflater);
170         MenuInflater mi = new MenuInflater(getActivity());
171         mi.inflate(R.menu.main, menu);
172         MenuItem log = menu.findItem(R.id.logs);
173         log.setOnMenuItemClickListener(new OnMenuItemClickListener() {
174             @Override
175             public boolean onMenuItemClick(MenuItem item) {
176                 showAllLogs();
177                 return true;
178             }
179         });
180         
181         MenuItem settings = menu.findItem(R.id.settings);
182         settings.setOnMenuItemClickListener(new OnMenuItemClickListener() {
183             void openSettingsNative(final MenuItem item) {
184                 setContent(new SettingsNativeFragment(), true, getString(R.string.settings));
185             }
186             
187             @Override
188             public boolean onMenuItemClick(final MenuItem item) {
189                 if (getActivity() instanceof FragmentActivity) {
190                     setContent(new SettingsFragment(), true, getString(R.string.settings));
191                 }
192                 else {
193                     openSettingsNative(item);
194                 }
195                 return true;
196             }
197         });
198     }
199
200 }