]> rtime.felk.cvut.cz Git - orte.git/blob - orte/contrib/shape_android/src/org/ocera/orte/shape4a/SubscriberFragment.java
5818e6d251e4bca5d54ea13b5ea34180168060c5
[orte.git] / orte / contrib / shape_android / src / org / ocera / orte / shape4a / SubscriberFragment.java
1 /**
2  * 
3  *      This file is part of shape4a.
4  *
5  *  shape4a is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  shape4a is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with shape4a.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 package org.ocera.orte.shape4a;
20
21 import android.app.AlertDialog;
22 import android.app.Dialog;
23 import android.content.DialogInterface;
24 import android.content.res.Configuration;
25 import android.os.Bundle;
26 import android.support.v4.app.DialogFragment;
27 import android.support.v4.app.Fragment;
28 import android.util.Log;
29 import android.view.LayoutInflater;
30 import android.view.Menu;
31 import android.view.MenuInflater;
32 import android.view.MenuItem;
33 import android.view.View;
34 import android.view.ViewGroup;
35 import android.widget.LinearLayout;
36 import android.widget.SeekBar;
37
38 import org.ocera.orte.DomainApp;
39 import org.ocera.orte.types.NtpTime;
40
41 /**
42  * Fragment containing subscribing workspace.
43  * 
44  * @author jiri hubacek <jiri.hubacek@gmail.com>
45  * @version %I%, %G%
46  * @since 1.0
47  */
48 public class SubscriberFragment extends Fragment {
49         private static final int MINSEPARATION_MAX = 5;
50         
51         private View rootView;
52         private SubscriberView subscriberView;
53         private DomainApp appDomain;
54         
55         private SubscriberSettingsDialogFragment subscriberSettingsDialogFragment = new SubscriberSettingsDialogFragment();
56         
57         /**
58          * Constructor just stores appDomain variable.
59          * 
60          * @param appDomain
61          * @since 1.0
62          */
63         public SubscriberFragment(DomainApp appDomain)
64         {
65                 this.appDomain = appDomain;
66         }
67         
68         /**
69          * When screen orientation is changed,
70          * scale recalculation is needed.
71          * 
72          * @since 1.0
73          */
74         @Override
75         public void onConfigurationChanged(Configuration newConfig)
76         {
77                 for (SubscriberElement e : this.subscriberView.elements) {
78                         e.setScale(getActivity().getWindowManager().getDefaultDisplay().getWidth(), getActivity().getWindowManager().getDefaultDisplay().getHeight());
79                 }
80         }
81         
82         /**
83          * Setting up workspace, adding publisherView
84          * and timer (handler) for drawing objects.
85          * 
86          * @since 1.0
87          */
88         @Override
89         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
90                 
91                 this.rootView = inflater.inflate(R.layout.fragment_subscriber, container, false);
92                 this.subscriberView = (SubscriberView) this.rootView.findViewById(R.id.subscriber_view);
93                 this.subscriberView.addElements(this.appDomain);
94                 for (SubscriberElement e : this.subscriberView.elements) {
95                         e.setScale(getActivity().getWindowManager().getDefaultDisplay().getWidth(), getActivity().getWindowManager().getDefaultDisplay().getHeight());
96                 }
97                 return this.rootView;
98         }
99         
100         /**
101          * Creating menu.
102          * 
103          * @since 1.0
104          */
105         @Override
106         public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
107         {
108                 inflater.inflate(R.menu.subscriber, menu);
109         }
110         
111         /**
112          * Setting up subscriber objects.
113          * 
114          * @since 1.0
115          */
116         @Override
117         public boolean onOptionsItemSelected(MenuItem item)
118         {
119                 int id = 0;
120                 switch (item.getItemId()) {
121                 case R.id.action_blue:
122                         id = 0;
123                         break;
124                 case R.id.action_green:
125                         id = 1;
126                         break;
127                 case R.id.action_red:
128                         id = 2;
129                         break;
130                 case R.id.action_black:
131                         id = 3;
132                         break;
133                 case R.id.action_yellow:
134                         id = 4;
135                         break;
136                 default:
137                 }
138                 
139                 subscriberSettingsDialogFragment.setId(id);
140                 subscriberSettingsDialogFragment.show(getFragmentManager(), "dialog");
141                 
142                 return super.onOptionsItemSelected(item);
143         }
144         
145         /**
146          * When click on shape in Action Bar menu.
147          * 
148          * @since 1.0
149          */
150         public class SubscriberSettingsDialogFragment extends DialogFragment
151         {
152                 private int eId, ms;
153                 private NtpTime minSeparation;
154                 private SeekBar seekMinSeparation;
155                 
156                 /**
157                  * Set wanted publisher's dialog.
158                  * 
159                  * @param i Index of publisher.
160                  * @since 1.0
161                  */
162                 public void setId(int i)
163                 {
164                         this.eId = i;
165                         this.minSeparation = subscriberView.elements.get(this.eId).getMinSeparation();
166                         this.ms = 0;
167                 }
168                 
169                 /**
170                  * Creating dialog for subscriber's settings.
171                  * 
172                  * @since 1.0
173                  */
174                 @Override
175                 public Dialog onCreateDialog(Bundle savedInstanceState)
176                 {
177                         AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
178                         builder.setTitle(PublisherShape.getColorName(this.eId) + " settings")
179                                 .setView(seekBar())
180                                 .setPositiveButton(R.string.dialog_OK, new DialogInterface.OnClickListener()
181                                 {
182                                         @Override
183                                         public void onClick(DialogInterface dialog, int which)
184                                         {
185                                                 minSeparation = new NtpTime(ms);
186                                                 subscriberView.elements.get(eId).setMinSeparation(minSeparation);
187                                                 subscriberView.elements.get(eId).setEnabled(true);
188                                         }
189                                 })
190                                 .setNeutralButton(subscriberView.elements.get(eId).getEnabled()?R.string.dialog_delete:R.string.dialog_add, new DialogInterface.OnClickListener()
191                                 {       
192                                         @Override
193                                         public void onClick(DialogInterface dialog, int which) {
194                                                 if (subscriberView.elements.get(eId).getEnabled()) {
195                                                         subscriberView.elements.get(eId).setEnabled(false);
196                                                 } else {
197                                                         minSeparation = new NtpTime(ms);
198                                                         subscriberView.elements.get(eId).setMinSeparation(minSeparation);
199                                                         subscriberView.elements.get(eId).setEnabled(true);
200                                                 }
201                                         }
202                                 })
203                                 .setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener()
204                                 {
205                                         @Override
206                                         public void onClick(DialogInterface dialog, int which)
207                                         {}
208                                 });
209                         return builder.create();
210                 }
211                 
212                 /**
213                  * SeekBar for setting-up min. separation of subscribers.
214                  * 
215                  * @return Layout of SeekBar.
216                  * @since 1.0
217                  */
218                 private View seekBar(){
219                         LinearLayout l = new LinearLayout(getActivity());
220                         l.setOrientation(LinearLayout.VERTICAL);
221                         
222                         seekMinSeparation = new SeekBar(getActivity());
223                         seekMinSeparation.setMax(MINSEPARATION_MAX);
224                         seekMinSeparation.setProgress(0);
225                         seekMinSeparation.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
226                         {
227                                 @Override
228                                 public void onStopTrackingTouch(SeekBar seekBar) {
229                                 }
230                                 
231                                 @Override
232                                 public void onStartTrackingTouch(SeekBar seekBar) {
233                                 }
234                                 
235                                 @Override
236                                 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
237                                 {
238                                         ms = progress;
239                                 }
240                         });
241                         
242                         l.addView(seekMinSeparation);
243                         return l;
244                 }
245         }
246 }