TileManager.cs 3.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
TileManager.cs
Copyright (C) 2015  Belledonne Communications, Grenoble, France
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
15 16 17
*/

using Microsoft.Phone.Shell;
18 19
using System;
using System.Collections.Generic;
20
using System.IO.IsolatedStorage;
21 22 23
using System.Linq;
using System.Text;
using System.Threading.Tasks;
24
using Windows.Storage;
25

26
namespace Linphone.Agents
27
{
28 29 30
    /// <summary>
    /// Utility class used to handle everything that is Tile related
    /// </summary>
31 32
    public class TileManager
    {
33 34 35
        private string TileCountKey = "TileCount";
        private IsolatedStorageSettings settings;

36
        private static TileManager singleton;
37 38 39
        /// <summary>
        /// Static instance to access the class.
        /// </summary>
40 41 42 43 44 45 46 47 48 49 50
        public static TileManager Instance
        {
            get
            {
                if (TileManager.singleton == null)
                    TileManager.singleton = new TileManager();

                return TileManager.singleton;
            }
        }

51 52 53
        /// <summary>
        /// Public constructor.
        /// </summary>
54 55
        public TileManager()
        {
56
            settings = IsolatedStorageSettings.ApplicationSettings;
57 58
        }

59
        /// <summary>
60
        /// Update the count to display on the Tile, and update the Tile with the new count
61
        /// </summary>
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
        /// <param name="toAdd"></param>
        public void UpdateCount(int toAdd)
        {
            int count = 0;
            try
            {
                count = (int)settings[TileCountKey];
                count += toAdd;
                settings[TileCountKey] = count;
            }
            catch (KeyNotFoundException)
            {
                settings.Add(TileCountKey, toAdd);
                count = toAdd;
            }
            settings.Save();
            UpdateTile(count);
        }

        private void UpdateTile(int count)
82 83
        {
            ShellTile tile = ShellTile.ActiveTiles.FirstOrDefault();
84 85 86 87 88 89
            if (tile != null)
            {
                var data = new IconicTileData();
                data.Count = count;
                tile.Update(data);
            }
90 91
        }

92 93 94
        /// <summary>
        /// Delete the missed call display from the live tile and the lock screen
        /// </summary>
95
        public void RemoveCountOnTile()
96
        {
97 98 99 100 101 102 103 104 105 106
            try
            {
                settings[TileCountKey] = 0;
            }
            catch (KeyNotFoundException)
            {
                settings.Add(TileCountKey, 0);
            }
            settings.Save();
            UpdateTile(0);
107 108 109
        }
    }
}