Debian Codegraph @

bus/haptic/haptic.go

raw
1 /*
2 Copyright 2014-2016 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 haptic can present notifications as a vibration pattern
18 // using the hfd-service interface
19 package haptic
20
21 import (
22 "gitlab.com/ubports/development/core/lomiri-push-service/bus"
23 "gitlab.com/ubports/development/core/lomiri-push-service/bus/accounts"
24 "gitlab.com/ubports/development/core/lomiri-push-service/click"
25 "gitlab.com/ubports/development/core/lomiri-push-service/click/cnotificationsettings"
26 "gitlab.com/ubports/development/core/lomiri-push-service/launch_helper"
27 "gitlab.com/ubports/development/core/lomiri-push-service/logger"
28 )
29
30 // hfd/haptic lives on a well-knwon bus.Address
31 var HfdBusAddress bus.Address = bus.Address{
32 Interface: "com.lomiri.hfd.Vibrator",
33 Path: "/com/lomiri/hfd",
34 Name: "com.lomiri.hfd",
35 }
36
37 // Haptic encapsulates info needed to call out to hfd-service
38 type Haptic struct {
39 bus bus.Endpoint
40 log logger.Logger
41 acc accounts.Accounts
42 fallback *launch_helper.Vibration
43 }
44
45 // New returns a new Haptic that'll use the provided bus.Endpoint
46 func New(endp bus.Endpoint, log logger.Logger, acc accounts.Accounts, fallback *launch_helper.Vibration) *Haptic {
47 return &Haptic{endp, log, acc, fallback}
48 }
49
50 var vibrateInSilentMode = cnotificationsettings.VibrateInSilentMode
51 var canUseVibrationsNotify = cnotificationsettings.CanUseVibrationsNotify
52
53 // Present presents the notification via a vibrate pattern
54 func (haptic *Haptic) Present(app *click.AppId, nid string, notification *launch_helper.Notification) bool {
55 if notification == nil {
56 panic("please check notification is not nil before calling present")
57 }
58
59 if !canUseVibrationsNotify(app) {
60 haptic.log.Debugf("[%s] vibrate disabled by user for this app.", nid)
61 return false
62 }
63
64 if haptic.acc.SilentMode() {
65 if !vibrateInSilentMode() {
66 haptic.log.Debugf("[%s] vibrate disabled by user when in Silent Mode.", nid)
67 return false
68 }
69 }
70
71 if !haptic.acc.Vibrate() {
72 haptic.log.Debugf("[%s] vibrate disabled by user.", nid)
73 return false
74 }
75
76 vib := notification.Vibration(haptic.fallback)
77 if vib == nil {
78 haptic.log.Debugf("[%s] notification has no Vibrate.", nid)
79 return false
80 }
81 pattern := vib.Pattern
82 repeat := vib.Repeat
83 if repeat == 0 {
84 repeat = 1
85 }
86 if len(pattern) == 0 {
87 haptic.log.Debugf("[%s] not enough information in the Vibrate to create a pattern", nid)
88 return false
89 }
90 haptic.log.Debugf("[%s] vibrating %d times to the tune of %v", nid, repeat, pattern)
91 err := haptic.bus.Call("vibratePattern", bus.Args(pattern, repeat))
92 if err != nil {
93 haptic.log.Errorf("[%s] call to vibratePattern returned %v", nid, err)
94 return false
95 }
96 return true
97 }