root / mobile / src / main / java / org / wannatrak / mobile / controller / CellIDFacade.java @ 37

View | Annotate | Download

1
package org.wannatrak.mobile.controller;
2
3
import org.wannatrak.mobile.model.Position;
4
import org.wannatrak.mobile.model.Cell;
5
import org.wannatrak.mobile.controller.cellid.dbclient.CellDBClient;
6
import org.wannatrak.mobile.controller.cellid.dbclient.CellDBOrgClient;
7
import org.wannatrak.mobile.controller.cellid.dbclient.OpenCellIDClient;
8
import org.wannatrak.mobile.controller.cellid.CellIDInfoProvider;
9
import org.wannatrak.mobile.controller.cellid.NokiaS40CellIDInfoProvider;
10
import org.wannatrak.mobile.controller.cellid.NokiaS60CellIDInfoProvider;
11
import org.wannatrak.mobile.controller.cellid.SonyEricssonCellIDInfoProvider;
12
13
public class CellIDFacade {
14
15
    private final CellDBClient[] dbClients;
16
    private final CellIDInfoProvider cellIDInfoProvider;
17
    private final Controller controller;
18
19
    public CellIDFacade(Controller controller) {
20
        this.controller = controller;
21
22
        dbClients = new CellDBClient[] {
23
                new OpenCellIDClient(controller),
24
                new CellDBOrgClient(controller)
25
        };
26
27
        CellIDInfoProvider nokiaS40CellIDInfoProvider = new NokiaS40CellIDInfoProvider();
28
        CellIDInfoProvider nokiaS60CellIDInfoProvider = new NokiaS60CellIDInfoProvider();
29
        CellIDInfoProvider seCellIDInfoProvider = new SonyEricssonCellIDInfoProvider();
30
        
31
        if (nokiaS40CellIDInfoProvider.doesWorkForPlatform()) {
32
            this.cellIDInfoProvider = nokiaS40CellIDInfoProvider;
33
34
            controller.showLog("Nokia S40 Cell ID Provider");
35
        } else if (nokiaS60CellIDInfoProvider.doesWorkForPlatform()) {
36
            this.cellIDInfoProvider = nokiaS60CellIDInfoProvider;
37
38
            controller.showLog("Nokia S60 Cell ID Provider");
39
        } else if (seCellIDInfoProvider.doesWorkForPlatform()) {
40
            this.cellIDInfoProvider = seCellIDInfoProvider;
41
42
            controller.showLog("Sony Ericsson Cell ID Provider");
43
        } else {
44
            this.cellIDInfoProvider = null;
45
46
            controller.showLog("Cell ID Provider not found");
47
        }
48
    }
49
50
    public boolean isCellIDAvailable() {
51
        return cellIDInfoProvider != null;
52
    }
53
54
    public Cell getCell() {
55
        if (!isCellIDAvailable()) {
56
            return null;
57
        }
58
        final Cell cell = cellIDInfoProvider.getCell();
59
        if (cell != null) {
60
            controller.showLog("Cell: " + cell.toString());
61
        }
62
        return cell;
63
    }
64
65
    public Position getPosition() {
66
        if (cellIDInfoProvider == null || dbClients == null || dbClients.length == 0) {
67
            return null;
68
        }
69
70
        final Cell cell = cellIDInfoProvider.getCell();
71
        if (cell != null) {
72
            controller.showLog("Cell: " + cell.toString());
73
        }
74
        if (!controller.setCellIfNew(cell)) {
75
            return null;
76
        }
77
78
        Position position = null;
79
        for (int i = 0; i < dbClients.length; i++) {
80
            position = dbClients[i].getPosition(
81
                    cell.cellID,
82
                    cell.mobileCountryCode,
83
                    cell.mobileNetworkCode,
84
                    cell.locationAreaCode
85
            );
86
            if (position != null) {
87
                if (position.getLatitude() == 0 && position.getLongitude() == 0) {
88
                    position = null;
89
                } else {
90
                    break;
91
                }
92
            }
93
        }
94
95
        return position;
96
    }
97
98
    public void postCellPosition(Cell cell, Position position) {
99
        for (int i = 0; i < dbClients.length; i++) {
100
             dbClients[i].postCellPosition(
101
                    cell.cellID,
102
                    cell.mobileCountryCode,
103
                    cell.mobileNetworkCode,
104
                    cell.locationAreaCode,
105
                    position.getLatitude(),
106
                    position.getLongitude(),
107
                    position.getTimestamp()
108
            );
109
        }
110
    }
111
}