OSDN Git Service

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