This repository has been archived by the owner on Jan 13, 2018. It is now read-only.
/
LegacyBar.cs
612 lines (509 loc) · 20.7 KB
/
LegacyBar.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/*
* Copyright (C) 2010 Johan Nilsson <http://markupartist.com>
*
* Original (https://github.com/johannilsson/android-actionbar) Ported to Mono for Android
* Copyright (C) 2013 LegacyBar - @Cheesebaron & @JamesMontemagno
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using Android.Content;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.OS;
using Android.Views;
using Android.Widget;
using Android.Util;
using LegacyBar.Library.BarActions;
namespace LegacyBar.Library.Bar
{
public sealed class LegacyBar : RelativeLayout, View.IOnClickListener, View.IOnLongClickListener
{
#region Fields
private LayoutInflater _inflater;
private RelativeLayout _barView;
private ImageView _logoView;
private View _backIndicator;
private TextView _titleView;
private LinearLayout _actionsView;
private ImageButton _homeBtn;
private RelativeLayout _homeLayout;
private ProgressBar _progress;
private RelativeLayout _titleLayout;
private OverflowLegacyBarAction _overflowLegacyBarAction;
//Used to track what we need to hide in the pop up menu.
public List<int> MenuItemsToHide = new List<int>();
#endregion
#region Properties
/// <summary>
/// According to Android documentation, all devices prior to Android 3.0 (API11) were required to have a menu button.
/// From API11 it was optional, and in API14 a method to detect if a button was present was presented.
/// Some devices like Samsung Galaxy S2 and S3 still have hardware buttons!
/// </summary>
public bool HasMenuButton
{
get
{
#if __ANDROID_14__
return ViewConfiguration.Get(Context).HasPermanentMenuKey;
#elif __ANDROID_11__
return false;
#else
//fallback
return ((int)Build.VERSION.SdkInt) < 11;
#endif
}
}
public LegacyBarTheme Theme { get; set; }
public bool LightIcons { get; set; }
public bool IsBottom { get; set; }
/// <summary>
/// Set the color of the seperators between Action Items
/// </summary>
public Color SeparatorColor
{
set
{
_actionsView.SetBackgroundColor(value);
_homeLayout.SetBackgroundColor(value);
}
}
public int SeparatorColorRaw
{
set
{
_actionsView.SetBackgroundResource(value);
_homeLayout.SetBackgroundResource(value);
}
}
/// <summary>
/// Set the drawable of the seperators between Action Items
/// </summary>
public Drawable SeparatorDrawable
{
set
{
_actionsView.SetBackgroundDrawable(value);
_homeLayout.SetBackgroundDrawable(value);
}
}
public int SeparatorDrawableRaw
{
set
{
_actionsView.SetBackgroundResource(value);
_homeLayout.SetBackgroundResource(value);
}
}
/// <summary>
/// Set the color of the Title in the Action Bar
/// </summary>
public Color TitleColor
{
set { _titleView.SetTextColor(value); }
}
public int TitleColorRaw
{
set {_titleView.SetTextColor(Resources.GetColor(value));}
}
/// <summary>
/// Set the title in the Action Bar
/// </summary>
public string Title
{
set { _titleView.Text = value; }
}
/// <summary>
/// Set the title in the Action Bar from a Resource Id
/// </summary>
public int TitleRaw
{
set { _titleView.SetText(value); }
}
/// <summary>
/// Set the background color of the Action Bar
/// </summary>
public Color BackgroundColor
{
set { SetBackgroundColor(value); }
}
public int BackgroundColorRaw
{
set {SetBackgroundColor(Resources.GetColor(value));}
}
/// <summary>
/// Set the background drawable of the Action Bar
/// </summary>
public Drawable BackgroundDrawable
{
set { SetBackgroundDrawable(value); }
}
public int BackgroundDrawableRaw
{
set { SetBackgroundResource(value);}
}
/// <summary>
/// Set the background drawable of the Action Bar Items
/// </summary>
public Drawable ItemBackgroundDrawable { get; set; }
public int ItemBackgroundDrawableRaw { get; set; }
/// <summary>
/// Returns the amount of Action Items in the Action Bar
/// </summary>
public int ActionCount
{
get { return _actionsView.ChildCount; }
}
/// <summary>
/// The visibility of the circular progress bar in the Action Bar
/// </summary>
public ViewStates ProgressBarVisibility
{
get { return _progress.Visibility; }
set { _progress.Visibility = value; }
}
#endregion
#region Events
public EventHandler TitleClick;
public EventHandler TitleLongClick;
#endregion
public LegacyBar(Context context, IAttributeSet attrs)
: base(context, attrs)
{
ResourceIdManager.UpdateIdValues();
_inflater = LayoutInflater.From(Context);
//_inflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
_barView = (RelativeLayout)_inflater.Inflate(Resource.Layout.actionbar, null);
AddView(_barView);
_logoView = _barView.FindViewById(Resource.Id.actionbar_home_logo) as ImageView;
_homeLayout = _barView.FindViewById<RelativeLayout>(Resource.Id.actionbar_home_bg);
_homeBtn = _barView.FindViewById<ImageButton>(Resource.Id.actionbar_home_btn);
_backIndicator = _barView.FindViewById(Resource.Id.actionbar_home_is_back);
_titleView = _barView.FindViewById<TextView>(Resource.Id.actionbar_title);
_actionsView = _barView.FindViewById<LinearLayout>(Resource.Id.actionbar_actions);
_progress = _barView.FindViewById<ProgressBar>(Resource.Id.actionbar_progress);
_titleLayout = _barView.FindViewById<RelativeLayout>(Resource.Id.actionbar_title_layout);
_overflowLegacyBarAction = new OverflowLegacyBarAction(Context);
//Custom Attributes (defined in Attrs.xml)
var a = Context.ObtainStyledAttributes(attrs,
Resource.Styleable.actionbar);
//grab theme attributes
Theme = (LegacyBarTheme)a.GetInt(Resource.Styleable.actionbar_theme, 0);
IsBottom = a.GetBoolean(Resource.Styleable.actionbar_is_bottom, false);
LightIcons = a.GetBoolean(Resource.Styleable.actionbar_light_icons, false);
//if we are not custom don't let them set it.
if (Theme != LegacyBarTheme.Custom)
{
LightIcons = Theme == LegacyBarTheme.HoloLight;
}
_overflowLegacyBarAction.SetIconColor(LightIcons);
var title = a.GetString(Resource.Styleable.actionbar_title);
if (null != title)
Title = title;
var titleColor = a.GetColor(Resource.Styleable.actionbar_title_color, Resources.GetColor(Resource.Color.actionbar_title));
TitleColor = titleColor;
var separatorColor = a.GetColor(Resource.Styleable.actionbar_separator, Resources.GetColor(Resource.Color.actionbar_separator));
_actionsView.SetBackgroundColor(separatorColor);
using (var background = a.GetDrawable(Resource.Styleable.actionbar_background)) //recycling the drawable immediately
{
if (null != background)
BackgroundDrawable = background;
}
var backgroundItem = a.GetDrawable(Resource.Styleable.actionbar_background_item);
if (null != backgroundItem)
ItemBackgroundDrawable = backgroundItem;
if (IsBottom)
{
LegacyBarUtils.SetBottomLegacyBarTheme(this, Theme);
}
else
{
LegacyBarUtils.SetLegacyBarTheme(this, Theme);
}
a.Recycle();
_titleView.Click += (s, e) => { if (null != TitleClick) TitleClick(s, e); };
_titleView.LongClick += (s, e) => { if (null != TitleLongClick) TitleLongClick(s, e); };
}
public void SetHomeAction(LegacyBarAction legacyBarAction)
{
_homeBtn.SetOnClickListener(this);
_homeBtn.Tag = legacyBarAction;
_homeBtn.SetImageResource(legacyBarAction.GetDrawable());
_homeLayout.Visibility = ViewStates.Visible;
_backIndicator.SetBackgroundResource(LightIcons ? Resource.Drawable.actionbar_back_indicator : Resource.Drawable.actionbar_back_indicator_dark);
if (null != ItemBackgroundDrawable)
{
_homeBtn.SetBackgroundDrawable(ItemBackgroundDrawable.GetConstantState().NewDrawable());
}
else if (ItemBackgroundDrawableRaw > 0)
{
_homeBtn.SetBackgroundResource(ItemBackgroundDrawableRaw);
}
((LayoutParams)_titleLayout.LayoutParameters).AddRule(LayoutRules.RightOf, Resource.Id.actionbar_home_bg);
}
public void ClearHomeAction()
{
_homeLayout.Visibility = ViewStates.Gone;
}
/// <summary>
/// Shows the provided logo to the left in the legacyBarAction bar.
///
/// This is ment to be used instead of the setHomeAction and does not draw
/// a divider to the left of the provided logo.
/// </summary>
/// <param name="resId">The drawable resource id</param>
public void SetHomeLogo(int resId)
{
// TODO: Add possibility to add an IntentAction as well.
_logoView.SetImageResource(resId);
_logoView.Visibility = ViewStates.Visible;
_homeLayout.Visibility = ViewStates.Gone;
if (null != ItemBackgroundDrawable)
{
_logoView.SetBackgroundDrawable(ItemBackgroundDrawable.GetConstantState().NewDrawable());
}
else if(ItemBackgroundDrawableRaw > 0)
{
_logoView.SetBackgroundResource(ItemBackgroundDrawableRaw);
}
((LayoutParams)_titleLayout.LayoutParameters).AddRule(LayoutRules.RightOf, Resource.Id.actionbar_home_logo);
}
/// <summary>
/// Emulating Honeycomb, setdisplayHomeAsUpEnabled takes a boolean
/// and toggles whether the "home" view should have a little triangle
/// indicating "up"
/// </summary>
/// <param name="show"></param>
public void SetDisplayHomeAsUpEnabled(bool show)
{
_backIndicator.Visibility = show ? ViewStates.Visible : ViewStates.Gone;
}
/// <summary>
/// Adds a list of Actions.
/// </summary>
/// <param name="actionList">List of Actions</param>
public void AddActions(LegacyBarUtils.ActionList actionList)
{
for (var i = 0; i < actionList.Count; i++)
{
AddAction(actionList.ElementAt(i));
}
}
/// <summary>
/// Adds a new Action.
/// </summary>
/// <param name="legacyBarAction">Action to add.</param>
public void AddAction(LegacyBarAction legacyBarAction)
{
AddAction(legacyBarAction, _actionsView.ChildCount);
}
/// <summary>
/// Adds new legacyBarAction in the overflow
/// </summary>
/// <param name="legacyBarAction">Action to add.</param>
public void AddOverflowAction(LegacyBarAction legacyBarAction)
{
var index = _actionsView.ChildCount;
_actionsView.AddView(InflateOverflowAction(legacyBarAction), index);
_overflowLegacyBarAction.Index = index;
}
/// <summary>
/// Adds a new Action at the specified index.
/// </summary>
/// <param name="legacyBarAction">the legacyBarAction to add</param>
/// <param name="index">the position at which to add the legacyBarAction</param>
public void AddAction(LegacyBarAction legacyBarAction, int index)
{
var addActionBar = false;
var hideAction = false;
if (!LegacyBarUtils.ActionFits(Context.Resources.DisplayMetrics.WidthPixels, Context.Resources.DisplayMetrics.Density, index, HasMenuButton, legacyBarAction.ActionType))
{
if (!HasMenuButton)
{
addActionBar = _overflowLegacyBarAction.ActionList.Count == 0;
_overflowLegacyBarAction.AddAction(legacyBarAction);
hideAction = true;
}
}
else
{
if (_overflowLegacyBarAction.ActionList.Count != 0)//exists
index = _overflowLegacyBarAction.Index;//bring it inside
hideAction = true;
_actionsView.AddView(InflateAction(legacyBarAction), index);
}
//simply put it in the menu items to hide if we are a menu item.
var taskAction = legacyBarAction as MenuItemLegacyBarAction;
if (taskAction != null && hideAction)
MenuItemsToHide.Add(taskAction.MenuItemId);
if (addActionBar)
AddOverflowAction(_overflowLegacyBarAction);
}
/// <summary>
/// Removes all legacyBarAction views from this legacyBarAction bar
/// </summary>
public void RemoveAllActions()
{
_actionsView.RemoveAllViews();
_overflowLegacyBarAction.ClearActions();
MenuItemsToHide.Clear();
}
/// <summary>
/// Remove a legacyBarAction from the legacyBarAction bar.
/// </summary>
/// <param name="index">position of legacyBarAction to remove</param>
public void RemoveActionAt(int index)
{
if (index < 1) return;
var menuItemAction = _actionsView.GetChildAt(index).Tag as MenuItemLegacyBarAction;
if (menuItemAction != null)
MenuItemsToHide.Remove(menuItemAction.MenuItemId);
_actionsView.RemoveViewAt(index);
}
/// <summary>
/// Remove a legacyBarAction from the legacyBarAction bar.
/// </summary>
/// <param name="id">position of legacyBarAction to remove</param>
public void RemoveActionAtMenuId(int id)
{
for (var i = 0; i < _actionsView.ChildCount; i++)
{
var view = _actionsView.GetChildAt(i);
if (view == null) continue;
var tag = view.Tag;
var actionBarAction = tag as MenuItemLegacyBarAction;
if (actionBarAction == null || id != actionBarAction.MenuItemId) continue;
MenuItemsToHide.Remove(actionBarAction.MenuItemId);
_actionsView.RemoveView(view);
}
}
/// <summary>
/// Remove a legacyBarAction from the legacyBarAction bar.
/// </summary>
/// <param name="legacyBarAction">The legacyBarAction to remove</param>
public void RemoveAction(LegacyBarAction legacyBarAction)
{
for (var i = 0; i < _actionsView.ChildCount; i++)
{
var view = _actionsView.GetChildAt(i);
if (view == null) continue;
var tag = view.Tag;
var actionBarAction = tag as LegacyBarAction;
if (actionBarAction == null || !actionBarAction.Equals(legacyBarAction)) continue;
var menuItemAction = tag as MenuItemLegacyBarAction;
if (menuItemAction != null)
MenuItemsToHide.Remove(menuItemAction.MenuItemId);
_actionsView.RemoveView(view);
}
}
/// <summary>
/// Inflates a View with the given Action.
/// </summary>
/// <param name="legacyBarAction">the legacyBarAction to inflate</param>
/// <returns>a view</returns>
private View InflateAction(LegacyBarAction legacyBarAction)
{
var view = _inflater.Inflate(Resource.Layout.actionbar_item, _actionsView, false);
if (null != ItemBackgroundDrawable)
{
view.SetBackgroundDrawable(ItemBackgroundDrawable.GetConstantState().NewDrawable());
}
else if (ItemBackgroundDrawableRaw > 0)
{
view.SetBackgroundResource(ItemBackgroundDrawableRaw);
}
var labelView =
view.FindViewById<ImageButton>(Resource.Id.actionbar_item);
labelView.SetImageResource(legacyBarAction.GetDrawable());
view.Tag = legacyBarAction;
view.SetOnClickListener(this);
view.SetOnLongClickListener(this);
if (legacyBarAction.PopUpMessage > 0)
view.ContentDescription = Resources.GetString(legacyBarAction.PopUpMessage);
return view;
}
private View InflateOverflowAction(LegacyBarAction legacyBarAction)
{
var view = _inflater.Inflate(Resource.Layout.overflowactionbar_item, _actionsView, false);
var labelView =
view.FindViewById<ImageButton>(Resource.Id.actionbar_item);
if (null != ItemBackgroundDrawable)
{
labelView.SetBackgroundDrawable(ItemBackgroundDrawable.GetConstantState().NewDrawable());
}
else if (ItemBackgroundDrawableRaw > 0)
{
labelView.SetBackgroundResource(ItemBackgroundDrawableRaw);
}
labelView.SetImageResource(legacyBarAction.GetDrawable());
var spinner = view.FindViewById<Spinner>(Resource.Id.overflow_spinner);
_overflowLegacyBarAction.OverflowSpinner = spinner;
labelView.Tag = legacyBarAction;
labelView.SetOnClickListener(this);
//view.SetOnLongClickListener(this);
return view;
}
#region Android OnClick Listeners and Event handlers
public void OnClick(View v)
{
var tag = v.Tag;
var action = tag as LegacyBarAction;
if (action != null)
{
action.PerformAction(v);
}
}
public bool OnLongClick(View v)
{
var tag = v.Tag;
var action = tag as LegacyBarAction;
if (action != null)
{
if (action.PopUpMessage == 0)
return true;
Toast.MakeText(Context, action.PopUpMessage, ToastLength.Short).Show();
return false;
}
return false;
}
#endregion
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (null != ItemBackgroundDrawable)
ItemBackgroundDrawable.Dispose();
if (null != _inflater)
_inflater.Dispose();
if (null != _overflowLegacyBarAction)
_overflowLegacyBarAction.Dispose();
ItemBackgroundDrawable = null;
_inflater = null;
_barView = null;
_logoView = null;
_backIndicator = null;
_titleView = null;
_actionsView = null;
_homeBtn = null;
_homeLayout = null;
_progress = null;
_titleLayout = null;
_overflowLegacyBarAction = null;
}
base.Dispose(disposing);
}
}
}