| 1 |
/* |
| 2 |
Copyright 2014 Canonical Ltd. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify it |
| 5 |
under the terms of the GNU General Public License version 3, as published |
| 6 |
by the Free Software Foundation. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, but |
| 9 |
WITHOUT ANY WARRANTY; without even the implied warranties of |
| 10 |
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 11 |
PURPOSE. See the GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License along |
| 14 |
with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
package launch_helper |
| 18 |
|
| 19 |
import ( |
| 20 |
"encoding/json" |
| 21 |
"time" |
| 22 |
|
| 23 |
"gitlab.com/ubports/development/core/lomiri-push-service/click" |
| 24 |
) |
| 25 |
|
| 26 |
// a Card is the usual “visual” presentation of a notification, used |
| 27 |
// for bubbles and the notification centre (neé messaging menu) |
| 28 |
type Card struct { |
| 29 |
Summary string `json:"summary"` // required for the card to be presented |
| 30 |
Body string `json:"body"` // defaults to empty |
| 31 |
Actions []string `json:"actions"` // if empty (default), bubble is non-clickable. More entries change it to be clickable and (for bubbles) snap-decisions. |
| 32 |
Icon string `json:"icon"` // an icon relating to the event being notified. Defaults to empty (no icon); a secondary icon relating to the application will be shown as well, irrespectively. |
| 33 |
RawTimestamp int `json:"timestamp"` // seconds since epoch, only used for persist (for now). Timestamp() returns this if non-zero, current timestamp otherwise. |
| 34 |
Persist bool `json:"persist"` // whether to show in notification centre; defaults to false |
| 35 |
Popup bool `json:"popup"` // whether to show in a bubble. Users can disable this, and can easily miss them, so don't rely on it exclusively. Defaults to false. |
| 36 |
} |
| 37 |
|
| 38 |
// an EmblemCounter puts a number on an emblem on an app's icon in the launcher |
| 39 |
type EmblemCounter struct { |
| 40 |
Count int32 `json:"count"` // the number to show on the emblem counter |
| 41 |
Visible bool `json:"visible"` // whether to show the emblem counter |
| 42 |
} |
| 43 |
|
| 44 |
// a Vibration generates a vibration in the form of a Pattern set in |
| 45 |
// duration a pattern of on off states, repeated a number of times |
| 46 |
type Vibration struct { |
| 47 |
Pattern []uint32 `json:"pattern"` |
| 48 |
Repeat uint32 `json:"repeat"` // defaults to 1. A value of zero is ignored (so it's like 1). |
| 49 |
} |
| 50 |
|
| 51 |
// a Notification can be any of the above |
| 52 |
type Notification struct { |
| 53 |
Card *Card `json:"card"` // defaults to nil (no card) |
| 54 |
RawSound json.RawMessage `json:"sound"` // a boolean, or the relative path to a sound file. Users can disable this, so don't rely on it exclusively. Defaults to empty (no sound). |
| 55 |
RawVibration json.RawMessage `json:"vibrate"` // users can disable this, blah blah. Can be Vibration, or boolean. Defaults to null (no vibration) |
| 56 |
EmblemCounter *EmblemCounter `json:"emblem-counter"` // puts a counter on an emblem in the launcher. Defaults to nil (no change to emblem counter). |
| 57 |
Tag string `json:"tag,omitempty"` // tag used for Clear/ListPersistent. |
| 58 |
} |
| 59 |
|
| 60 |
// HelperOutput is the expected output of a helper |
| 61 |
type HelperOutput struct { |
| 62 |
Message json.RawMessage `json:"message,omitempty"` // what to put in the post office's queue |
| 63 |
Notification *Notification `json:"notification,omitempty"` // what to present to the user |
| 64 |
} |
| 65 |
|
| 66 |
// HelperResult is the result of a helper run for a particular app id |
| 67 |
type HelperResult struct { |
| 68 |
HelperOutput |
| 69 |
Input *HelperInput |
| 70 |
} |
| 71 |
|
| 72 |
// HelperInput is what's passed in to a helper for it to work |
| 73 |
type HelperInput struct { |
| 74 |
kind string |
| 75 |
App *click.AppId |
| 76 |
NotificationId string |
| 77 |
Payload json.RawMessage |
| 78 |
} |
| 79 |
|
| 80 |
// Timestamp() returns RawTimestamp if non-zero. If it's zero, returns |
| 81 |
// the current time as second since epoch. |
| 82 |
func (card *Card) Timestamp() int64 { |
| 83 |
if card.RawTimestamp == 0 { |
| 84 |
return time.Now().Unix() |
| 85 |
} else { |
| 86 |
return int64(card.RawTimestamp) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
func (notification *Notification) Vibration(fallback *Vibration) *Vibration { |
| 91 |
var b bool |
| 92 |
var vib *Vibration |
| 93 |
|
| 94 |
if notification.RawVibration == nil { |
| 95 |
return nil |
| 96 |
} |
| 97 |
if json.Unmarshal(notification.RawVibration, &b) == nil { |
| 98 |
if !b { |
| 99 |
return nil |
| 100 |
} else { |
| 101 |
return fallback |
| 102 |
} |
| 103 |
} |
| 104 |
if json.Unmarshal(notification.RawVibration, &vib) != nil { |
| 105 |
return nil |
| 106 |
} |
| 107 |
if len(vib.Pattern) == 0 { |
| 108 |
return nil |
| 109 |
} |
| 110 |
|
| 111 |
return vib |
| 112 |
} |
| 113 |
|
| 114 |
func (notification *Notification) Sound(fallback string) string { |
| 115 |
var b bool |
| 116 |
var s string |
| 117 |
|
| 118 |
if notification.RawSound == nil { |
| 119 |
return "" |
| 120 |
} |
| 121 |
if json.Unmarshal(notification.RawSound, &b) == nil { |
| 122 |
if !b { |
| 123 |
return "" |
| 124 |
} else { |
| 125 |
return fallback |
| 126 |
} |
| 127 |
} |
| 128 |
if json.Unmarshal(notification.RawSound, &s) != nil { |
| 129 |
return "" |
| 130 |
} |
| 131 |
|
| 132 |
return s |
| 133 |
} |