OSDN Git Service

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