├── .classpath
├── .project
├── AndroidManifest.xml
├── LICENSE.txt
├── README.txt
├── bin
├── AndroidManifest.xml
├── androidpn-client.apk
├── classes.dex
├── classes
│ └── org
│ │ └── androidpn
│ │ ├── client
│ │ ├── ConnectivityReceiver.class
│ │ ├── Constants.class
│ │ ├── InvalidFormatException.class
│ │ ├── LogUtil.class
│ │ ├── NotificationDetailsActivity$1.class
│ │ ├── NotificationDetailsActivity.class
│ │ ├── NotificationIQ.class
│ │ ├── NotificationIQProvider.class
│ │ ├── NotificationPacketListener.class
│ │ ├── NotificationReceiver.class
│ │ ├── NotificationService$1.class
│ │ ├── NotificationService$2.class
│ │ ├── NotificationService$3.class
│ │ ├── NotificationService$TaskSubmitter.class
│ │ ├── NotificationService$TaskTracker.class
│ │ ├── NotificationService.class
│ │ ├── NotificationSettingsActivity$1.class
│ │ ├── NotificationSettingsActivity.class
│ │ ├── Notifier.class
│ │ ├── PersistentConnectionListener.class
│ │ ├── PhoneStateChangeListener.class
│ │ ├── ReconnectionThread$1.class
│ │ ├── ReconnectionThread.class
│ │ ├── ServiceManager$1.class
│ │ ├── ServiceManager.class
│ │ ├── XmppManager$1.class
│ │ ├── XmppManager$ConnectTask.class
│ │ ├── XmppManager$LoginTask.class
│ │ ├── XmppManager$RegisterTask$1.class
│ │ ├── XmppManager$RegisterTask.class
│ │ └── XmppManager.class
│ │ └── demoapp
│ │ ├── BuildConfig.class
│ │ ├── DemoAppActivity$1.class
│ │ ├── DemoAppActivity.class
│ │ ├── R$attr.class
│ │ ├── R$drawable.class
│ │ ├── R$id.class
│ │ ├── R$layout.class
│ │ ├── R$raw.class
│ │ ├── R$string.class
│ │ └── R.class
├── jarlist.cache
├── res
│ ├── drawable-hdpi
│ │ └── icon.png
│ ├── drawable-ldpi
│ │ └── icon.png
│ ├── drawable-mdpi
│ │ └── icon.png
│ └── drawable
│ │ └── notification.png
└── resources.ap_
├── build.properties
├── build.xml
├── gen
└── org
│ └── androidpn
│ └── demoapp
│ ├── BuildConfig.java
│ └── R.java
├── libs
└── asmack.jar
├── project.properties
├── res
├── drawable-hdpi
│ └── icon.png
├── drawable-ldpi
│ └── icon.png
├── drawable-mdpi
│ └── icon.png
├── drawable
│ └── notification.png
├── layout
│ └── main.xml
├── raw
│ └── androidpn.properties
└── values
│ └── strings.xml
└── src
└── org
└── androidpn
├── client
├── ConnectivityReceiver.java
├── Constants.java
├── InvalidFormatException.java
├── LogUtil.java
├── NotificationDetailsActivity.java
├── NotificationIQ.java
├── NotificationIQProvider.java
├── NotificationPacketListener.java
├── NotificationReceiver.java
├── NotificationService.java
├── NotificationSettingsActivity.java
├── Notifier.java
├── PersistentConnectionListener.java
├── PhoneStateChangeListener.java
├── ReconnectionThread.java
├── ServiceManager.java
└── XmppManager.java
└── demoapp
└── DemoAppActivity.java
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | androidpn-client
4 |
5 |
6 |
7 |
8 |
9 | com.android.ide.eclipse.adt.ResourceManagerBuilder
10 |
11 |
12 |
13 |
14 | com.android.ide.eclipse.adt.PreCompilerBuilder
15 |
16 |
17 |
18 |
19 | org.eclipse.jdt.core.javabuilder
20 |
21 |
22 |
23 |
24 | com.android.ide.eclipse.adt.ApkBuilder
25 |
26 |
27 |
28 |
29 |
30 | com.android.ide.eclipse.adt.AndroidNature
31 | org.eclipse.jdt.core.javanature
32 |
33 |
34 |
--------------------------------------------------------------------------------
/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
14 |
15 |
17 |
18 |
21 |
22 |
23 |
24 |
25 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 |
2 | Apache License
3 | Version 2.0, January 2004
4 | http://www.apache.org/licenses/
5 |
6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7 |
8 | 1. Definitions.
9 |
10 | "License" shall mean the terms and conditions for use, reproduction,
11 | and distribution as defined by Sections 1 through 9 of this document.
12 |
13 | "Licensor" shall mean the copyright owner or entity authorized by
14 | the copyright owner that is granting the License.
15 |
16 | "Legal Entity" shall mean the union of the acting entity and all
17 | other entities that control, are controlled by, or are under common
18 | control with that entity. For the purposes of this definition,
19 | "control" means (i) the power, direct or indirect, to cause the
20 | direction or management of such entity, whether by contract or
21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
22 | outstanding shares, or (iii) beneficial ownership of such entity.
23 |
24 | "You" (or "Your") shall mean an individual or Legal Entity
25 | exercising permissions granted by this License.
26 |
27 | "Source" form shall mean the preferred form for making modifications,
28 | including but not limited to software source code, documentation
29 | source, and configuration files.
30 |
31 | "Object" form shall mean any form resulting from mechanical
32 | transformation or translation of a Source form, including but
33 | not limited to compiled object code, generated documentation,
34 | and conversions to other media types.
35 |
36 | "Work" shall mean the work of authorship, whether in Source or
37 | Object form, made available under the License, as indicated by a
38 | copyright notice that is included in or attached to the work
39 | (an example is provided in the Appendix below).
40 |
41 | "Derivative Works" shall mean any work, whether in Source or Object
42 | form, that is based on (or derived from) the Work and for which the
43 | editorial revisions, annotations, elaborations, or other modifications
44 | represent, as a whole, an original work of authorship. For the purposes
45 | of this License, Derivative Works shall not include works that remain
46 | separable from, or merely link (or bind by name) to the interfaces of,
47 | the Work and Derivative Works thereof.
48 |
49 | "Contribution" shall mean any work of authorship, including
50 | the original version of the Work and any modifications or additions
51 | to that Work or Derivative Works thereof, that is intentionally
52 | submitted to Licensor for inclusion in the Work by the copyright owner
53 | or by an individual or Legal Entity authorized to submit on behalf of
54 | the copyright owner. For the purposes of this definition, "submitted"
55 | means any form of electronic, verbal, or written communication sent
56 | to the Licensor or its representatives, including but not limited to
57 | communication on electronic mailing lists, source code control systems,
58 | and issue tracking systems that are managed by, or on behalf of, the
59 | Licensor for the purpose of discussing and improving the Work, but
60 | excluding communication that is conspicuously marked or otherwise
61 | designated in writing by the copyright owner as "Not a Contribution."
62 |
63 | "Contributor" shall mean Licensor and any individual or Legal Entity
64 | on behalf of whom a Contribution has been received by Licensor and
65 | subsequently incorporated within the Work.
66 |
67 | 2. Grant of Copyright License. Subject to the terms and conditions of
68 | this License, each Contributor hereby grants to You a perpetual,
69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70 | copyright license to reproduce, prepare Derivative Works of,
71 | publicly display, publicly perform, sublicense, and distribute the
72 | Work and such Derivative Works in Source or Object form.
73 |
74 | 3. Grant of Patent License. Subject to the terms and conditions of
75 | this License, each Contributor hereby grants to You a perpetual,
76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77 | (except as stated in this section) patent license to make, have made,
78 | use, offer to sell, sell, import, and otherwise transfer the Work,
79 | where such license applies only to those patent claims licensable
80 | by such Contributor that are necessarily infringed by their
81 | Contribution(s) alone or by combination of their Contribution(s)
82 | with the Work to which such Contribution(s) was submitted. If You
83 | institute patent litigation against any entity (including a
84 | cross-claim or counterclaim in a lawsuit) alleging that the Work
85 | or a Contribution incorporated within the Work constitutes direct
86 | or contributory patent infringement, then any patent licenses
87 | granted to You under this License for that Work shall terminate
88 | as of the date such litigation is filed.
89 |
90 | 4. Redistribution. You may reproduce and distribute copies of the
91 | Work or Derivative Works thereof in any medium, with or without
92 | modifications, and in Source or Object form, provided that You
93 | meet the following conditions:
94 |
95 | (a) You must give any other recipients of the Work or
96 | Derivative Works a copy of this License; and
97 |
98 | (b) You must cause any modified files to carry prominent notices
99 | stating that You changed the files; and
100 |
101 | (c) You must retain, in the Source form of any Derivative Works
102 | that You distribute, all copyright, patent, trademark, and
103 | attribution notices from the Source form of the Work,
104 | excluding those notices that do not pertain to any part of
105 | the Derivative Works; and
106 |
107 | (d) If the Work includes a "NOTICE" text file as part of its
108 | distribution, then any Derivative Works that You distribute must
109 | include a readable copy of the attribution notices contained
110 | within such NOTICE file, excluding those notices that do not
111 | pertain to any part of the Derivative Works, in at least one
112 | of the following places: within a NOTICE text file distributed
113 | as part of the Derivative Works; within the Source form or
114 | documentation, if provided along with the Derivative Works; or,
115 | within a display generated by the Derivative Works, if and
116 | wherever such third-party notices normally appear. The contents
117 | of the NOTICE file are for informational purposes only and
118 | do not modify the License. You may add Your own attribution
119 | notices within Derivative Works that You distribute, alongside
120 | or as an addendum to the NOTICE text from the Work, provided
121 | that such additional attribution notices cannot be construed
122 | as modifying the License.
123 |
124 | You may add Your own copyright statement to Your modifications and
125 | may provide additional or different license terms and conditions
126 | for use, reproduction, or distribution of Your modifications, or
127 | for any such Derivative Works as a whole, provided Your use,
128 | reproduction, and distribution of the Work otherwise complies with
129 | the conditions stated in this License.
130 |
131 | 5. Submission of Contributions. Unless You explicitly state otherwise,
132 | any Contribution intentionally submitted for inclusion in the Work
133 | by You to the Licensor shall be under the terms and conditions of
134 | this License, without any additional terms or conditions.
135 | Notwithstanding the above, nothing herein shall supersede or modify
136 | the terms of any separate license agreement you may have executed
137 | with Licensor regarding such Contributions.
138 |
139 | 6. Trademarks. This License does not grant permission to use the trade
140 | names, trademarks, service marks, or product names of the Licensor,
141 | except as required for reasonable and customary use in describing the
142 | origin of the Work and reproducing the content of the NOTICE file.
143 |
144 | 7. Disclaimer of Warranty. Unless required by applicable law or
145 | agreed to in writing, Licensor provides the Work (and each
146 | Contributor provides its Contributions) on an "AS IS" BASIS,
147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148 | implied, including, without limitation, any warranties or conditions
149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150 | PARTICULAR PURPOSE. You are solely responsible for determining the
151 | appropriateness of using or redistributing the Work and assume any
152 | risks associated with Your exercise of permissions under this License.
153 |
154 | 8. Limitation of Liability. In no event and under no legal theory,
155 | whether in tort (including negligence), contract, or otherwise,
156 | unless required by applicable law (such as deliberate and grossly
157 | negligent acts) or agreed to in writing, shall any Contributor be
158 | liable to You for damages, including any direct, indirect, special,
159 | incidental, or consequential damages of any character arising as a
160 | result of this License or out of the use or inability to use the
161 | Work (including but not limited to damages for loss of goodwill,
162 | work stoppage, computer failure or malfunction, or any and all
163 | other commercial damages or losses), even if such Contributor
164 | has been advised of the possibility of such damages.
165 |
166 | 9. Accepting Warranty or Additional Liability. While redistributing
167 | the Work or Derivative Works thereof, You may choose to offer,
168 | and charge a fee for, acceptance of support, warranty, indemnity,
169 | or other liability obligations and/or rights consistent with this
170 | License. However, in accepting such obligations, You may act only
171 | on Your own behalf and on Your sole responsibility, not on behalf
172 | of any other Contributor, and only if You agree to indemnify,
173 | defend, and hold each Contributor harmless for any liability
174 | incurred by, or claims asserted against, such Contributor by reason
175 | of your accepting any such warranty or additional liability.
176 |
177 | END OF TERMS AND CONDITIONS
178 |
--------------------------------------------------------------------------------
/README.txt:
--------------------------------------------------------------------------------
1 | ANDROIDPN-CLIENT README
2 | =======================
3 | http://androidpn.sourceforge.net/
4 |
5 | Push Notification Service for Android
6 |
7 | This is an open source project to provide push notification support for Android
8 | -- a xmpp based notification server and a client tool kit.
9 |
10 |
11 | how to use
12 | http://www.devdiv.com/thread-101586-1-1.html
13 |
--------------------------------------------------------------------------------
/bin/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
14 |
15 |
17 |
18 |
21 |
22 |
23 |
24 |
25 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/bin/androidpn-client.apk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/androidpn-client.apk
--------------------------------------------------------------------------------
/bin/classes.dex:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes.dex
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/client/ConnectivityReceiver.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/client/ConnectivityReceiver.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/client/Constants.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/client/Constants.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/client/InvalidFormatException.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/client/InvalidFormatException.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/client/LogUtil.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/client/LogUtil.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/client/NotificationDetailsActivity$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/client/NotificationDetailsActivity$1.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/client/NotificationDetailsActivity.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/client/NotificationDetailsActivity.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/client/NotificationIQ.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/client/NotificationIQ.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/client/NotificationIQProvider.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/client/NotificationIQProvider.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/client/NotificationPacketListener.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/client/NotificationPacketListener.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/client/NotificationReceiver.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/client/NotificationReceiver.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/client/NotificationService$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/client/NotificationService$1.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/client/NotificationService$2.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/client/NotificationService$2.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/client/NotificationService$3.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/client/NotificationService$3.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/client/NotificationService$TaskSubmitter.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/client/NotificationService$TaskSubmitter.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/client/NotificationService$TaskTracker.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/client/NotificationService$TaskTracker.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/client/NotificationService.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/client/NotificationService.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/client/NotificationSettingsActivity$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/client/NotificationSettingsActivity$1.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/client/NotificationSettingsActivity.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/client/NotificationSettingsActivity.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/client/Notifier.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/client/Notifier.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/client/PersistentConnectionListener.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/client/PersistentConnectionListener.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/client/PhoneStateChangeListener.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/client/PhoneStateChangeListener.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/client/ReconnectionThread$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/client/ReconnectionThread$1.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/client/ReconnectionThread.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/client/ReconnectionThread.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/client/ServiceManager$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/client/ServiceManager$1.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/client/ServiceManager.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/client/ServiceManager.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/client/XmppManager$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/client/XmppManager$1.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/client/XmppManager$ConnectTask.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/client/XmppManager$ConnectTask.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/client/XmppManager$LoginTask.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/client/XmppManager$LoginTask.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/client/XmppManager$RegisterTask$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/client/XmppManager$RegisterTask$1.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/client/XmppManager$RegisterTask.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/client/XmppManager$RegisterTask.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/client/XmppManager.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/client/XmppManager.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/demoapp/BuildConfig.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/demoapp/BuildConfig.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/demoapp/DemoAppActivity$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/demoapp/DemoAppActivity$1.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/demoapp/DemoAppActivity.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/demoapp/DemoAppActivity.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/demoapp/R$attr.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/demoapp/R$attr.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/demoapp/R$drawable.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/demoapp/R$drawable.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/demoapp/R$id.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/demoapp/R$id.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/demoapp/R$layout.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/demoapp/R$layout.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/demoapp/R$raw.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/demoapp/R$raw.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/demoapp/R$string.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/demoapp/R$string.class
--------------------------------------------------------------------------------
/bin/classes/org/androidpn/demoapp/R.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/classes/org/androidpn/demoapp/R.class
--------------------------------------------------------------------------------
/bin/jarlist.cache:
--------------------------------------------------------------------------------
1 | # cache for current jar dependecy. DO NOT EDIT.
2 | # format is
3 | # Encoding is UTF-8
4 |
--------------------------------------------------------------------------------
/bin/res/drawable-hdpi/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/res/drawable-hdpi/icon.png
--------------------------------------------------------------------------------
/bin/res/drawable-ldpi/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/res/drawable-ldpi/icon.png
--------------------------------------------------------------------------------
/bin/res/drawable-mdpi/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/res/drawable-mdpi/icon.png
--------------------------------------------------------------------------------
/bin/res/drawable/notification.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/res/drawable/notification.png
--------------------------------------------------------------------------------
/bin/resources.ap_:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/bin/resources.ap_
--------------------------------------------------------------------------------
/build.properties:
--------------------------------------------------------------------------------
1 | # Project
2 | project.name=androidpn-client
3 | project.version=0.5.0
4 |
5 | # Android SDK
6 | android.sdk.jar=c:/Android/android-sdk/platforms/android-3/android.jar
7 |
--------------------------------------------------------------------------------
/build.xml:
--------------------------------------------------------------------------------
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 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
--------------------------------------------------------------------------------
/gen/org/androidpn/demoapp/BuildConfig.java:
--------------------------------------------------------------------------------
1 | /** Automatically generated file. DO NOT MODIFY */
2 | package org.androidpn.demoapp;
3 |
4 | public final class BuildConfig {
5 | public final static boolean DEBUG = true;
6 | }
--------------------------------------------------------------------------------
/gen/org/androidpn/demoapp/R.java:
--------------------------------------------------------------------------------
1 | /* AUTO-GENERATED FILE. DO NOT MODIFY.
2 | *
3 | * This class was automatically generated by the
4 | * aapt tool from the resource data it found. It
5 | * should not be modified by hand.
6 | */
7 |
8 | package org.androidpn.demoapp;
9 |
10 | public final class R {
11 | public static final class attr {
12 | }
13 | public static final class drawable {
14 | public static final int icon=0x7f020000;
15 | public static final int notification=0x7f020001;
16 | }
17 | public static final class id {
18 | public static final int btn_settings=0x7f060000;
19 | }
20 | public static final class layout {
21 | public static final int main=0x7f030000;
22 | }
23 | public static final class raw {
24 | public static final int androidpn=0x7f040000;
25 | }
26 | public static final class string {
27 | public static final int app_name=0x7f050000;
28 | public static final int hello=0x7f050001;
29 | public static final int settings=0x7f050002;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/libs/asmack.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/libs/asmack.jar
--------------------------------------------------------------------------------
/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-10
15 |
--------------------------------------------------------------------------------
/res/drawable-hdpi/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/res/drawable-hdpi/icon.png
--------------------------------------------------------------------------------
/res/drawable-ldpi/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/res/drawable-ldpi/icon.png
--------------------------------------------------------------------------------
/res/drawable-mdpi/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/res/drawable-mdpi/icon.png
--------------------------------------------------------------------------------
/res/drawable/notification.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-demo-xmpp-androidpn/69686c2d4d5097cb54e8c89adf35ce3f1a37db90/res/drawable/notification.png
--------------------------------------------------------------------------------
/res/layout/main.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
17 |
24 |
25 |
--------------------------------------------------------------------------------
/res/raw/androidpn.properties:
--------------------------------------------------------------------------------
1 | apiKey=1234567890
2 | xmppHost=192.168.89.75
3 | xmppPort=5222
4 |
--------------------------------------------------------------------------------
/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | AndroidpnClient
4 | Hello Android, DemoAppActivity!
5 | Settings
6 |
7 |
--------------------------------------------------------------------------------
/src/org/androidpn/client/ConnectivityReceiver.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010 Moduad Co., Ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.androidpn.client;
17 |
18 | import android.content.BroadcastReceiver;
19 | import android.content.Context;
20 | import android.content.Intent;
21 | import android.net.ConnectivityManager;
22 | import android.net.NetworkInfo;
23 | import android.util.Log;
24 |
25 | /**
26 | * A broadcast receiver to handle the changes in network connectiion states.
27 | *
28 | * @author Sehwan Noh (devnoh@gmail.com)
29 | */
30 | public class ConnectivityReceiver extends BroadcastReceiver {
31 |
32 | private static final String LOGTAG = LogUtil
33 | .makeLogTag(ConnectivityReceiver.class);
34 |
35 | private NotificationService notificationService;
36 |
37 | public ConnectivityReceiver(NotificationService notificationService) {
38 | this.notificationService = notificationService;
39 | }
40 |
41 | @Override
42 | public void onReceive(Context context, Intent intent) {
43 | Log.d(LOGTAG, "ConnectivityReceiver.onReceive()...");
44 | String action = intent.getAction();
45 | Log.d(LOGTAG, "action=" + action);
46 |
47 | ConnectivityManager connectivityManager = (ConnectivityManager) context
48 | .getSystemService(Context.CONNECTIVITY_SERVICE);
49 |
50 | NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
51 |
52 | if (networkInfo != null) {
53 | Log.d(LOGTAG, "Network Type = " + networkInfo.getTypeName());
54 | Log.d(LOGTAG, "Network State = " + networkInfo.getState());
55 | if (networkInfo.isConnected()) {
56 | Log.i(LOGTAG, "Network connected");
57 | notificationService.connect();
58 | }
59 | } else {
60 | Log.e(LOGTAG, "Network unavailable");
61 | notificationService.disconnect();
62 | }
63 | }
64 |
65 | }
66 |
--------------------------------------------------------------------------------
/src/org/androidpn/client/Constants.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010 Moduad Co., Ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.androidpn.client;
17 |
18 | /**
19 | * Static constants for this package.
20 | *
21 | * @author Sehwan Noh (devnoh@gmail.com)
22 | */
23 | public class Constants {
24 |
25 | public static final String SHARED_PREFERENCE_NAME = "client_preferences";
26 |
27 | // PREFERENCE KEYS
28 |
29 | public static final String CALLBACK_ACTIVITY_PACKAGE_NAME = "CALLBACK_ACTIVITY_PACKAGE_NAME";
30 |
31 | public static final String CALLBACK_ACTIVITY_CLASS_NAME = "CALLBACK_ACTIVITY_CLASS_NAME";
32 |
33 | public static final String API_KEY = "API_KEY";
34 |
35 | public static final String VERSION = "VERSION";
36 |
37 | public static final String XMPP_HOST = "XMPP_HOST";
38 |
39 | public static final String XMPP_PORT = "XMPP_PORT";
40 |
41 | public static final String XMPP_USERNAME = "XMPP_USERNAME";
42 |
43 | public static final String XMPP_PASSWORD = "XMPP_PASSWORD";
44 |
45 | // public static final String USER_KEY = "USER_KEY";
46 |
47 | public static final String DEVICE_ID = "DEVICE_ID";
48 |
49 | public static final String EMULATOR_DEVICE_ID = "EMULATOR_DEVICE_ID";
50 |
51 | public static final String NOTIFICATION_ICON = "NOTIFICATION_ICON";
52 |
53 | public static final String SETTINGS_NOTIFICATION_ENABLED = "SETTINGS_NOTIFICATION_ENABLED";
54 |
55 | public static final String SETTINGS_SOUND_ENABLED = "SETTINGS_SOUND_ENABLED";
56 |
57 | public static final String SETTINGS_VIBRATE_ENABLED = "SETTINGS_VIBRATE_ENABLED";
58 |
59 | public static final String SETTINGS_TOAST_ENABLED = "SETTINGS_TOAST_ENABLED";
60 |
61 | // NOTIFICATION FIELDS
62 |
63 | public static final String NOTIFICATION_ID = "NOTIFICATION_ID";
64 |
65 | public static final String NOTIFICATION_API_KEY = "NOTIFICATION_API_KEY";
66 |
67 | public static final String NOTIFICATION_TITLE = "NOTIFICATION_TITLE";
68 |
69 | public static final String NOTIFICATION_MESSAGE = "NOTIFICATION_MESSAGE";
70 |
71 | public static final String NOTIFICATION_URI = "NOTIFICATION_URI";
72 |
73 | // INTENT ACTIONS
74 |
75 | public static final String ACTION_SHOW_NOTIFICATION = "org.androidpn.client.SHOW_NOTIFICATION";
76 |
77 | public static final String ACTION_NOTIFICATION_CLICKED = "org.androidpn.client.NOTIFICATION_CLICKED";
78 |
79 | public static final String ACTION_NOTIFICATION_CLEARED = "org.androidpn.client.NOTIFICATION_CLEARED";
80 |
81 | }
82 |
--------------------------------------------------------------------------------
/src/org/androidpn/client/InvalidFormatException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010 Moduad Co., Ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.androidpn.client;
17 |
18 | /**
19 | * Runtime exceptions produced by wrong meta-data settings.
20 | *
21 | * @author Sehwan Noh (devnoh@gmail.com)
22 | */
23 | public class InvalidFormatException extends RuntimeException {
24 |
25 | private static final long serialVersionUID = 1L;
26 |
27 | public InvalidFormatException() {
28 | super();
29 | }
30 |
31 | public InvalidFormatException(String message) {
32 | super(message);
33 | }
34 |
35 | public InvalidFormatException(Throwable cause) {
36 | super(cause);
37 | }
38 |
39 | public InvalidFormatException(String message, Throwable cause) {
40 | super(message, cause);
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/src/org/androidpn/client/LogUtil.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2010 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.androidpn.client;
17 |
18 | /**
19 | * Utility class for LogCat.
20 | *
21 | * @author Sehwan Noh (devnoh@gmail.com)
22 | */
23 | public class LogUtil {
24 |
25 | @SuppressWarnings("unchecked")
26 | public static String makeLogTag(Class cls) {
27 | return "Androidpn_" + cls.getSimpleName();
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/src/org/androidpn/client/NotificationDetailsActivity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010 Moduad Co., Ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.androidpn.client;
17 |
18 | import android.app.Activity;
19 | import android.content.Context;
20 | import android.content.Intent;
21 | import android.content.SharedPreferences;
22 | import android.graphics.Typeface;
23 | import android.net.Uri;
24 | import android.os.Bundle;
25 | import android.util.Log;
26 | import android.view.Gravity;
27 | import android.view.View;
28 | import android.widget.Button;
29 | import android.widget.LinearLayout;
30 | import android.widget.TextView;
31 |
32 | /**
33 | * Activity for displaying the notification details view.
34 | *
35 | * @author Sehwan Noh (devnoh@gmail.com)
36 | */
37 | public class NotificationDetailsActivity extends Activity {
38 |
39 | private static final String LOGTAG = LogUtil
40 | .makeLogTag(NotificationDetailsActivity.class);
41 |
42 | private String callbackActivityPackageName;
43 |
44 | private String callbackActivityClassName;
45 |
46 | public NotificationDetailsActivity() {
47 | }
48 |
49 | @Override
50 | public void onCreate(Bundle savedInstanceState) {
51 | super.onCreate(savedInstanceState);
52 |
53 | SharedPreferences sharedPrefs = this.getSharedPreferences(
54 | Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
55 | callbackActivityPackageName = sharedPrefs.getString(
56 | Constants.CALLBACK_ACTIVITY_PACKAGE_NAME, "");
57 | callbackActivityClassName = sharedPrefs.getString(
58 | Constants.CALLBACK_ACTIVITY_CLASS_NAME, "");
59 |
60 | Intent intent = getIntent();
61 | String notificationId = intent
62 | .getStringExtra(Constants.NOTIFICATION_ID);
63 | String notificationApiKey = intent
64 | .getStringExtra(Constants.NOTIFICATION_API_KEY);
65 | String notificationTitle = intent
66 | .getStringExtra(Constants.NOTIFICATION_TITLE);
67 | String notificationMessage = intent
68 | .getStringExtra(Constants.NOTIFICATION_MESSAGE);
69 | String notificationUri = intent
70 | .getStringExtra(Constants.NOTIFICATION_URI);
71 |
72 | Log.d(LOGTAG, "notificationId=" + notificationId);
73 | Log.d(LOGTAG, "notificationApiKey=" + notificationApiKey);
74 | Log.d(LOGTAG, "notificationTitle=" + notificationTitle);
75 | Log.d(LOGTAG, "notificationMessage=" + notificationMessage);
76 | Log.d(LOGTAG, "notificationUri=" + notificationUri);
77 |
78 | // Display display = getWindowManager().getDefaultDisplay();
79 | // View rootView;
80 | // if (display.getWidth() > display.getHeight()) {
81 | // rootView = null;
82 | // } else {
83 | // rootView = null;
84 | // }
85 |
86 | View rootView = createView(notificationTitle, notificationMessage,
87 | notificationUri);
88 | setContentView(rootView);
89 | }
90 |
91 | private View createView(final String title, final String message,
92 | final String uri) {
93 |
94 | LinearLayout linearLayout = new LinearLayout(this);
95 | linearLayout.setBackgroundColor(0xffeeeeee);
96 | linearLayout.setOrientation(LinearLayout.VERTICAL);
97 | linearLayout.setPadding(5, 5, 5, 5);
98 | LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
99 | LinearLayout.LayoutParams.FILL_PARENT,
100 | LinearLayout.LayoutParams.FILL_PARENT);
101 | linearLayout.setLayoutParams(layoutParams);
102 |
103 | TextView textTitle = new TextView(this);
104 | textTitle.setText(title);
105 | textTitle.setTextSize(18);
106 | // textTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
107 | textTitle.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
108 | textTitle.setTextColor(0xff000000);
109 | textTitle.setGravity(Gravity.CENTER);
110 |
111 | layoutParams = new LinearLayout.LayoutParams(
112 | LinearLayout.LayoutParams.FILL_PARENT,
113 | LinearLayout.LayoutParams.WRAP_CONTENT);
114 | layoutParams.setMargins(30, 30, 30, 0);
115 | textTitle.setLayoutParams(layoutParams);
116 | linearLayout.addView(textTitle);
117 |
118 | TextView textDetails = new TextView(this);
119 | textDetails.setText(message);
120 | textDetails.setTextSize(14);
121 | // textTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
122 | textDetails.setTextColor(0xff333333);
123 | textDetails.setGravity(Gravity.CENTER);
124 |
125 | layoutParams = new LinearLayout.LayoutParams(
126 | LinearLayout.LayoutParams.FILL_PARENT,
127 | LinearLayout.LayoutParams.WRAP_CONTENT);
128 | layoutParams.setMargins(30, 10, 30, 20);
129 | textDetails.setLayoutParams(layoutParams);
130 | linearLayout.addView(textDetails);
131 |
132 | Button okButton = new Button(this);
133 | okButton.setText("Ok");
134 | okButton.setWidth(100);
135 |
136 | okButton.setOnClickListener(new View.OnClickListener() {
137 | public void onClick(View view) {
138 | Intent intent;
139 | if (uri != null
140 | && uri.length() > 0
141 | && (uri.startsWith("http:") || uri.startsWith("https:")
142 | || uri.startsWith("tel:") || uri
143 | .startsWith("geo:"))) {
144 | intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
145 | } else {
146 | intent = new Intent().setClassName(
147 | callbackActivityPackageName,
148 | callbackActivityClassName);
149 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
150 | intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
151 | intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
152 | // intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
153 | // intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
154 | // intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
155 | }
156 |
157 | NotificationDetailsActivity.this.startActivity(intent);
158 | NotificationDetailsActivity.this.finish();
159 | }
160 | });
161 |
162 | LinearLayout innerLayout = new LinearLayout(this);
163 | innerLayout.setGravity(Gravity.CENTER);
164 | innerLayout.addView(okButton);
165 |
166 | linearLayout.addView(innerLayout);
167 |
168 | return linearLayout;
169 | }
170 |
171 | // protected void onPause() {
172 | // super.onPause();
173 | // finish();
174 | // }
175 | //
176 | // protected void onStop() {
177 | // super.onStop();
178 | // finish();
179 | // }
180 | //
181 | // protected void onSaveInstanceState(Bundle outState) {
182 | // super.onSaveInstanceState(outState);
183 | // }
184 | //
185 | // protected void onNewIntent(Intent intent) {
186 | // setIntent(intent);
187 | // }
188 |
189 | }
190 |
--------------------------------------------------------------------------------
/src/org/androidpn/client/NotificationIQ.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010 Moduad Co., Ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.androidpn.client;
17 |
18 | import org.jivesoftware.smack.packet.IQ;
19 |
20 | /**
21 | * This class represents a notifcatin IQ packet.
22 | *
23 | * @author Sehwan Noh (devnoh@gmail.com)
24 | */
25 | public class NotificationIQ extends IQ {
26 |
27 | private String id;
28 |
29 | private String apiKey;
30 |
31 | private String title;
32 |
33 | private String message;
34 |
35 | private String uri;
36 |
37 | public NotificationIQ() {
38 | }
39 |
40 | @Override
41 | public String getChildElementXML() {
42 | StringBuilder buf = new StringBuilder();
43 | buf.append("<").append("notification").append(" xmlns=\"").append(
44 | "androidpn:iq:notification").append("\">");
45 | if (id != null) {
46 | buf.append("").append(id).append("");
47 | }
48 | buf.append("").append("notification").append("> ");
49 | return buf.toString();
50 | }
51 |
52 | public String getId() {
53 | return id;
54 | }
55 |
56 | public void setId(String id) {
57 | this.id = id;
58 | }
59 |
60 | public String getApiKey() {
61 | return apiKey;
62 | }
63 |
64 | public void setApiKey(String apiKey) {
65 | this.apiKey = apiKey;
66 | }
67 |
68 | public String getTitle() {
69 | return title;
70 | }
71 |
72 | public void setTitle(String title) {
73 | this.title = title;
74 | }
75 |
76 | public String getMessage() {
77 | return message;
78 | }
79 |
80 | public void setMessage(String message) {
81 | this.message = message;
82 | }
83 |
84 | public String getUri() {
85 | return uri;
86 | }
87 |
88 | public void setUri(String url) {
89 | this.uri = url;
90 | }
91 |
92 | }
93 |
--------------------------------------------------------------------------------
/src/org/androidpn/client/NotificationIQProvider.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010 Moduad Co., Ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.androidpn.client;
17 |
18 | import org.jivesoftware.smack.packet.IQ;
19 | import org.jivesoftware.smack.provider.IQProvider;
20 | import org.xmlpull.v1.XmlPullParser;
21 |
22 | import android.util.Log;
23 |
24 | /**
25 | * This class parses incoming IQ packets to NotificationIQ objects.
26 | *解析Push收到的IQ数据包为NotificationIQ对象
27 | * @author Sehwan Noh (devnoh@gmail.com)
28 | */
29 | public class NotificationIQProvider implements IQProvider {
30 |
31 | private static final String LOGTAG = LogUtil.makeLogTag(NotificationIQProvider.class);
32 |
33 | public NotificationIQProvider() {
34 | }
35 |
36 | @Override
37 | public IQ parseIQ(XmlPullParser parser) throws Exception {
38 | NotificationIQ notification = new NotificationIQ();
39 | for (boolean done = false; !done;) {
40 | int eventType = parser.next();
41 | Log.d(LOGTAG, "parseIQ.....eventType: "+eventType);
42 | if (eventType == XmlPullParser.START_TAG) {
43 | if ("id".equals(parser.getName())) {
44 | notification.setId(parser.nextText());
45 | }
46 | if ("apiKey".equals(parser.getName())) {
47 | notification.setApiKey(parser.nextText());
48 | }
49 | if ("title".equals(parser.getName())) {
50 | notification.setTitle(parser.nextText());
51 | }
52 | if ("message".equals(parser.getName())) {
53 | notification.setMessage(parser.nextText());
54 | }
55 | if ("uri".equals(parser.getName())) {
56 | notification.setUri(parser.nextText());
57 | }
58 | } else if (eventType == XmlPullParser.END_TAG
59 | && "notification".equals(parser.getName())) {
60 | done = true;
61 | }
62 | }
63 |
64 | return notification;
65 | }
66 |
67 | }
68 |
--------------------------------------------------------------------------------
/src/org/androidpn/client/NotificationPacketListener.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010 Moduad Co., Ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.androidpn.client;
17 |
18 | import org.jivesoftware.smack.PacketListener;
19 | import org.jivesoftware.smack.packet.Packet;
20 |
21 | import android.content.Intent;
22 | import android.util.Log;
23 |
24 | /**
25 | * This class notifies the receiver of incoming notifcation packets asynchronously.
26 | *
27 | * @author Sehwan Noh (devnoh@gmail.com)
28 | */
29 | public class NotificationPacketListener implements PacketListener {
30 |
31 | private static final String LOGTAG = LogUtil
32 | .makeLogTag(NotificationPacketListener.class);
33 |
34 | private final XmppManager xmppManager;
35 |
36 | public NotificationPacketListener(XmppManager xmppManager) {
37 | this.xmppManager = xmppManager;
38 | }
39 |
40 | /**
41 | * 对于服务器push过来的信息进行处理
42 | * 从传过来的Packet(数据包)里获取自己需要的数据
43 | */
44 | @Override
45 | public void processPacket(Packet packet) {
46 | Log.d(LOGTAG, "NotificationPacketListener.processPacket()...");
47 | Log.d(LOGTAG, "packet.toXML()=" + packet.toXML());
48 |
49 | if (packet instanceof NotificationIQ) {
50 | NotificationIQ notification = (NotificationIQ) packet;
51 |
52 | if (notification.getChildElementXML().contains(
53 | "androidpn:iq:notification")) {
54 | String notificationId = notification.getId();
55 | String notificationApiKey = notification.getApiKey();
56 | String notificationTitle = notification.getTitle();
57 | String notificationMessage = notification.getMessage();
58 | // String notificationTicker = notification.getTicker();
59 | String notificationUri = notification.getUri();
60 |
61 | Intent intent = new Intent(Constants.ACTION_SHOW_NOTIFICATION);
62 | intent.putExtra(Constants.NOTIFICATION_ID, notificationId);
63 | intent.putExtra(Constants.NOTIFICATION_API_KEY,
64 | notificationApiKey);
65 | intent.putExtra(Constants.NOTIFICATION_TITLE,
66 | notificationTitle);
67 | intent.putExtra(Constants.NOTIFICATION_MESSAGE,
68 | notificationMessage);
69 | intent.putExtra(Constants.NOTIFICATION_URI, notificationUri);
70 | // intent.setData(Uri.parse((new StringBuilder(
71 | // "notif://notification.androidpn.org/")).append(
72 | // notificationApiKey).append("/").append(
73 | // System.currentTimeMillis()).toString()));
74 |
75 | xmppManager.getContext().sendBroadcast(intent);
76 | }
77 | }
78 |
79 | }
80 |
81 | }
82 |
--------------------------------------------------------------------------------
/src/org/androidpn/client/NotificationReceiver.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010 Moduad Co., Ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.androidpn.client;
17 |
18 | import android.content.BroadcastReceiver;
19 | import android.content.Context;
20 | import android.content.Intent;
21 | import android.util.Log;
22 |
23 | /**
24 | * Broadcast receiver that handles push notification messages from the server.
25 | * This should be registered as receiver in AndroidManifest.xml.
26 | *
27 | * @author Sehwan Noh (devnoh@gmail.com)
28 | */
29 | public final class NotificationReceiver extends BroadcastReceiver {
30 |
31 | private static final String LOGTAG = LogUtil
32 | .makeLogTag(NotificationReceiver.class);
33 |
34 | // private NotificationService notificationService;
35 |
36 | public NotificationReceiver() {
37 | }
38 |
39 | // public NotificationReceiver(NotificationService notificationService) {
40 | // this.notificationService = notificationService;
41 | // }
42 |
43 | @Override
44 | public void onReceive(Context context, Intent intent) {
45 | Log.d(LOGTAG, "NotificationReceiver.onReceive()...");
46 | String action = intent.getAction();
47 | Log.d(LOGTAG, "action=" + action);
48 |
49 | if (Constants.ACTION_SHOW_NOTIFICATION.equals(action)) {
50 | String notificationId = intent
51 | .getStringExtra(Constants.NOTIFICATION_ID);
52 | String notificationApiKey = intent
53 | .getStringExtra(Constants.NOTIFICATION_API_KEY);
54 | String notificationTitle = intent
55 | .getStringExtra(Constants.NOTIFICATION_TITLE);
56 | String notificationMessage = intent
57 | .getStringExtra(Constants.NOTIFICATION_MESSAGE);
58 | String notificationUri = intent
59 | .getStringExtra(Constants.NOTIFICATION_URI);
60 |
61 | Log.d(LOGTAG, "notificationId=" + notificationId);
62 | Log.d(LOGTAG, "notificationApiKey=" + notificationApiKey);
63 | Log.d(LOGTAG, "notificationTitle=" + notificationTitle);
64 | Log.d(LOGTAG, "notificationMessage=" + notificationMessage);
65 | Log.d(LOGTAG, "notificationUri=" + notificationUri);
66 |
67 | Notifier notifier = new Notifier(context);
68 | notifier.notify(notificationId, notificationApiKey,
69 | notificationTitle, notificationMessage, notificationUri);
70 | }
71 |
72 | // } else if (Constants.ACTION_NOTIFICATION_CLICKED.equals(action)) {
73 | // String notificationId = intent
74 | // .getStringExtra(Constants.NOTIFICATION_ID);
75 | // String notificationApiKey = intent
76 | // .getStringExtra(Constants.NOTIFICATION_API_KEY);
77 | // String notificationTitle = intent
78 | // .getStringExtra(Constants.NOTIFICATION_TITLE);
79 | // String notificationMessage = intent
80 | // .getStringExtra(Constants.NOTIFICATION_MESSAGE);
81 | // String notificationUri = intent
82 | // .getStringExtra(Constants.NOTIFICATION_URI);
83 | //
84 | // Log.e(LOGTAG, "notificationId=" + notificationId);
85 | // Log.e(LOGTAG, "notificationApiKey=" + notificationApiKey);
86 | // Log.e(LOGTAG, "notificationTitle=" + notificationTitle);
87 | // Log.e(LOGTAG, "notificationMessage=" + notificationMessage);
88 | // Log.e(LOGTAG, "notificationUri=" + notificationUri);
89 | //
90 | // Intent detailsIntent = new Intent();
91 | // detailsIntent.setClass(context, NotificationDetailsActivity.class);
92 | // detailsIntent.putExtras(intent.getExtras());
93 | // // detailsIntent.putExtra(Constants.NOTIFICATION_ID, notificationId);
94 | // // detailsIntent.putExtra(Constants.NOTIFICATION_API_KEY, notificationApiKey);
95 | // // detailsIntent.putExtra(Constants.NOTIFICATION_TITLE, notificationTitle);
96 | // // detailsIntent.putExtra(Constants.NOTIFICATION_MESSAGE, notificationMessage);
97 | // // detailsIntent.putExtra(Constants.NOTIFICATION_URI, notificationUri);
98 | // detailsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
99 | // detailsIntent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
100 | //
101 | // try {
102 | // context.startActivity(detailsIntent);
103 | // } catch (ActivityNotFoundException e) {
104 | // Toast toast = Toast.makeText(context,
105 | // "No app found to handle this request",
106 | // Toast.LENGTH_LONG);
107 | // toast.show();
108 | // }
109 | //
110 | // } else if (Constants.ACTION_NOTIFICATION_CLEARED.equals(action)) {
111 | // //
112 | // }
113 |
114 | }
115 |
116 | }
117 |
--------------------------------------------------------------------------------
/src/org/androidpn/client/NotificationService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010 Moduad Co., Ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.androidpn.client;
17 |
18 | import java.util.Random;
19 | import java.util.concurrent.ExecutorService;
20 | import java.util.concurrent.Executors;
21 | import java.util.concurrent.Future;
22 |
23 | import android.app.Service;
24 | import android.content.BroadcastReceiver;
25 | import android.content.Context;
26 | import android.content.Intent;
27 | import android.content.IntentFilter;
28 | import android.content.SharedPreferences;
29 | import android.content.SharedPreferences.Editor;
30 | import android.os.IBinder;
31 | import android.telephony.PhoneStateListener;
32 | import android.telephony.TelephonyManager;
33 | import android.util.Log;
34 |
35 | /**
36 | * Service that continues to run in background and respond to the push
37 | * notification events from the server. This should be registered as service
38 | * in AndroidManifest.xml.
39 | *
40 | * @author Sehwan Noh (devnoh@gmail.com)
41 | */
42 | public class NotificationService extends Service {
43 |
44 | private static final String LOGTAG = LogUtil
45 | .makeLogTag(NotificationService.class);
46 |
47 | public static final String SERVICE_NAME = "org.androidpn.client.NotificationService";
48 |
49 | private TelephonyManager telephonyManager;
50 |
51 | // private WifiManager wifiManager;
52 | //
53 | // private ConnectivityManager connectivityManager;
54 |
55 | //通知栏的广播接收者,用于广播来自服务器的推送信息
56 | private BroadcastReceiver notificationReceiver;
57 |
58 | //网络连接性的广播接收者,处理网络连接状态的改变
59 | private BroadcastReceiver connectivityReceiver;
60 | //监听手机连接状态的改变
61 | private PhoneStateListener phoneStateListener;
62 |
63 | //对提交的Runnable任务进行执行操作的服务
64 | private ExecutorService executorService;
65 |
66 | //任务的提交者,用于提交一个个Runnable的任务
67 | private TaskSubmitter taskSubmitter;
68 | //任务监听,用于监听有多少个任务正在运行(running)
69 | private TaskTracker taskTracker;
70 |
71 | private XmppManager xmppManager;
72 |
73 | private SharedPreferences sharedPrefs;
74 | //设备ID
75 | private String deviceId;
76 |
77 | public NotificationService() {
78 | notificationReceiver = new NotificationReceiver();
79 | connectivityReceiver = new ConnectivityReceiver(this);
80 | phoneStateListener = new PhoneStateChangeListener(this);
81 | executorService = Executors.newSingleThreadExecutor();
82 | taskSubmitter = new TaskSubmitter(this);
83 | taskTracker = new TaskTracker(this);
84 | }
85 |
86 | @Override
87 | public void onCreate() {
88 | Log.d(LOGTAG, "onCreate()...");
89 | telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
90 | // wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
91 | // connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
92 |
93 | sharedPrefs = getSharedPreferences(Constants.SHARED_PREFERENCE_NAME,
94 | Context.MODE_PRIVATE);
95 |
96 | // Get deviceId
97 | deviceId = telephonyManager.getDeviceId();
98 | // Log.d(LOGTAG, "deviceId=" + deviceId);
99 | Editor editor = sharedPrefs.edit();
100 | editor.putString(Constants.DEVICE_ID, deviceId);
101 | editor.commit();
102 |
103 | // If running on an emulator
104 | if (deviceId == null || deviceId.trim().length() == 0
105 | || deviceId.matches("0+")) {
106 | if (sharedPrefs.contains("EMULATOR_DEVICE_ID")) {
107 | deviceId = sharedPrefs.getString(Constants.EMULATOR_DEVICE_ID,
108 | "");
109 | } else {
110 | deviceId = (new StringBuilder("EMU")).append(
111 | (new Random(System.currentTimeMillis())).nextLong())
112 | .toString();
113 | editor.putString(Constants.EMULATOR_DEVICE_ID, deviceId);
114 | editor.commit();
115 | }
116 | }
117 | Log.d(LOGTAG, "deviceId=" + deviceId);
118 |
119 | xmppManager = new XmppManager(this);
120 |
121 | taskSubmitter.submit(new Runnable() {
122 | public void run() {
123 | NotificationService.this.start();
124 | }
125 | });
126 | }
127 |
128 | @Override
129 | public void onStart(Intent intent, int startId) {
130 | Log.d(LOGTAG, "onStart()...");
131 | }
132 |
133 | @Override
134 | public void onDestroy() {
135 | Log.d(LOGTAG, "onDestroy()...");
136 | stop();
137 | }
138 |
139 | @Override
140 | public IBinder onBind(Intent intent) {
141 | Log.d(LOGTAG, "onBind()...");
142 | return null;
143 | }
144 |
145 | @Override
146 | public void onRebind(Intent intent) {
147 | Log.d(LOGTAG, "onRebind()...");
148 | }
149 |
150 | @Override
151 | public boolean onUnbind(Intent intent) {
152 | Log.d(LOGTAG, "onUnbind()...");
153 | return true;
154 | }
155 |
156 | public static Intent getIntent() {
157 | return new Intent(SERVICE_NAME);
158 | }
159 |
160 | public ExecutorService getExecutorService() {
161 | return executorService;
162 | }
163 |
164 | public TaskSubmitter getTaskSubmitter() {
165 | return taskSubmitter;
166 | }
167 |
168 | public TaskTracker getTaskTracker() {
169 | return taskTracker;
170 | }
171 |
172 | public XmppManager getXmppManager() {
173 | return xmppManager;
174 | }
175 |
176 | public SharedPreferences getSharedPreferences() {
177 | return sharedPrefs;
178 | }
179 |
180 | public String getDeviceId() {
181 | return deviceId;
182 | }
183 |
184 | /**
185 | * 连接到XMPP服务器
186 | */
187 | public void connect() {
188 | Log.d(LOGTAG, "connect()...");
189 | taskSubmitter.submit(new Runnable() {
190 | public void run() {
191 | NotificationService.this.getXmppManager().connect();
192 | }
193 | });
194 | }
195 |
196 | /**
197 | * 断开与XMPP服务器的连接
198 | */
199 | public void disconnect() {
200 | Log.d(LOGTAG, "disconnect()...");
201 | taskSubmitter.submit(new Runnable() {
202 | public void run() {
203 | NotificationService.this.getXmppManager().disconnect();
204 | }
205 | });
206 | }
207 |
208 | /**
209 | * 注册通知的广播接收者
210 | */
211 | private void registerNotificationReceiver() {
212 | IntentFilter filter = new IntentFilter();
213 | filter.addAction(Constants.ACTION_SHOW_NOTIFICATION);
214 | filter.addAction(Constants.ACTION_NOTIFICATION_CLICKED);
215 | filter.addAction(Constants.ACTION_NOTIFICATION_CLEARED);
216 | registerReceiver(notificationReceiver, filter);
217 | }
218 |
219 | private void unregisterNotificationReceiver() {
220 | unregisterReceiver(notificationReceiver);
221 | }
222 |
223 | /**
224 | * 注册网络连接状态的广播接收者
225 | */
226 | private void registerConnectivityReceiver() {
227 | Log.d(LOGTAG, "registerConnectivityReceiver()...");
228 | telephonyManager.listen(phoneStateListener,
229 | PhoneStateListener.LISTEN_DATA_CONNECTION_STATE); //注册网络状态改变监听
230 | IntentFilter filter = new IntentFilter();
231 | // filter.addAction(android.net.wifi.WifiManager.NETWORK_STATE_CHANGED_ACTION);
232 | filter.addAction(android.net.ConnectivityManager.CONNECTIVITY_ACTION);
233 | registerReceiver(connectivityReceiver, filter);
234 | }
235 |
236 | private void unregisterConnectivityReceiver() {
237 | Log.d(LOGTAG, "unregisterConnectivityReceiver()...");
238 | telephonyManager.listen(phoneStateListener,
239 | PhoneStateListener.LISTEN_NONE); //注销网络状态改变监听
240 | unregisterReceiver(connectivityReceiver);
241 | }
242 |
243 | private void start() {
244 | Log.d(LOGTAG, "start()...");
245 | registerNotificationReceiver(); //注册通知的广播接收者
246 | registerConnectivityReceiver(); //注册网络连接状态的广播接收者
247 | // Intent intent = getIntent();
248 | // startService(intent);
249 | xmppManager.connect(); //连接到XMPP服务器
250 | }
251 |
252 | private void stop() {
253 | Log.d(LOGTAG, "stop()...");
254 | unregisterNotificationReceiver();
255 | unregisterConnectivityReceiver();
256 | xmppManager.disconnect(); //断开与XMPP服务器的连接
257 | executorService.shutdown(); //释放执行服务的资源
258 | }
259 |
260 | /**
261 | * Class for summiting a new runnable task.
262 | */
263 | public class TaskSubmitter {
264 |
265 | final NotificationService notificationService;
266 |
267 | public TaskSubmitter(NotificationService notificationService) {
268 | this.notificationService = notificationService;
269 | }
270 |
271 | @SuppressWarnings("unchecked")
272 | public Future submit(Runnable task) {
273 | Future result = null;
274 | if (!notificationService.getExecutorService().isTerminated()
275 | && !notificationService.getExecutorService().isShutdown()
276 | && task != null) {
277 | result = notificationService.getExecutorService().submit(task);
278 | }
279 | return result;
280 | }
281 |
282 | }
283 |
284 | /**
285 | * Class for monitoring the running task count.
286 | */
287 | public class TaskTracker {
288 |
289 | final NotificationService notificationService;
290 |
291 | public int count;
292 |
293 | public TaskTracker(NotificationService notificationService) {
294 | this.notificationService = notificationService;
295 | this.count = 0;
296 | }
297 |
298 | public void increase() {
299 | synchronized (notificationService.getTaskTracker()) {
300 | notificationService.getTaskTracker().count++;
301 | Log.d(LOGTAG, "Incremented task count to " + count);
302 | }
303 | }
304 |
305 | public void decrease() {
306 | synchronized (notificationService.getTaskTracker()) {
307 | notificationService.getTaskTracker().count--;
308 | Log.d(LOGTAG, "Decremented task count to " + count);
309 | }
310 | }
311 |
312 | }
313 |
314 | }
315 |
--------------------------------------------------------------------------------
/src/org/androidpn/client/NotificationSettingsActivity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2010 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.androidpn.client;
17 |
18 | import android.content.Context;
19 | import android.os.Bundle;
20 | import android.preference.CheckBoxPreference;
21 | import android.preference.Preference;
22 | import android.preference.PreferenceActivity;
23 | import android.preference.PreferenceManager;
24 | import android.preference.PreferenceScreen;
25 | import android.util.Log;
26 |
27 | /**
28 | * Activity for displaying the notification setting view.
29 | *
30 | * @author Sehwan Noh (devnoh@gmail.com)
31 | */
32 | public class NotificationSettingsActivity extends PreferenceActivity {
33 |
34 | private static final String LOGTAG = LogUtil
35 | .makeLogTag(NotificationSettingsActivity.class);
36 |
37 | public NotificationSettingsActivity() {
38 | }
39 |
40 | @Override
41 | protected void onCreate(Bundle savedInstanceState) {
42 | super.onCreate(savedInstanceState);
43 | setPreferenceScreen(createPreferenceHierarchy());
44 | setPreferenceDependencies();
45 |
46 | CheckBoxPreference notifyPref = (CheckBoxPreference) getPreferenceManager()
47 | .findPreference(Constants.SETTINGS_NOTIFICATION_ENABLED);
48 | if (notifyPref.isChecked()) {
49 | notifyPref.setTitle("Notifications Enabled");
50 | } else {
51 | notifyPref.setTitle("Notifications Disabled");
52 | }
53 | }
54 |
55 | private PreferenceScreen createPreferenceHierarchy() {
56 | Log.d(LOGTAG, "createSettingsPreferenceScreen()...");
57 |
58 | PreferenceManager preferenceManager = getPreferenceManager();
59 | preferenceManager
60 | .setSharedPreferencesName(Constants.SHARED_PREFERENCE_NAME);
61 | preferenceManager.setSharedPreferencesMode(Context.MODE_PRIVATE);
62 |
63 | PreferenceScreen root = preferenceManager.createPreferenceScreen(this);
64 |
65 | // PreferenceCategory prefCat = new PreferenceCategory(this);
66 | // // inlinePrefCat.setTitle("");
67 | // root.addPreference(prefCat);
68 |
69 | CheckBoxPreference notifyPref = new CheckBoxPreference(this);
70 | notifyPref.setKey(Constants.SETTINGS_NOTIFICATION_ENABLED);
71 | notifyPref.setTitle("Notifications Enabled");
72 | notifyPref.setSummaryOn("Receive push messages");
73 | notifyPref.setSummaryOff("Do not receive push messages");
74 | notifyPref.setDefaultValue(Boolean.TRUE);
75 | notifyPref
76 | .setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
77 | public boolean onPreferenceChange(Preference preference,
78 | Object newValue) {
79 | boolean checked = Boolean.valueOf(newValue.toString());
80 | if (checked) {
81 | preference.setTitle("Notifications Enabled");
82 | } else {
83 | preference.setTitle("Notifications Disabled");
84 | }
85 | return true;
86 | }
87 | });
88 |
89 | CheckBoxPreference soundPref = new CheckBoxPreference(this);
90 | soundPref.setKey(Constants.SETTINGS_SOUND_ENABLED);
91 | soundPref.setTitle("Sound");
92 | soundPref.setSummary("Play a sound for notifications");
93 | soundPref.setDefaultValue(Boolean.TRUE);
94 | // soundPref.setDependency(Constants.SETTINGS_NOTIFICATION_ENABLED);
95 |
96 | CheckBoxPreference vibratePref = new CheckBoxPreference(this);
97 | vibratePref.setKey(Constants.SETTINGS_VIBRATE_ENABLED);
98 | vibratePref.setTitle("Vibrate");
99 | vibratePref.setSummary("Vibrate the phone for notifications");
100 | vibratePref.setDefaultValue(Boolean.TRUE);
101 | // vibratePref.setDependency(Constants.SETTINGS_NOTIFICATION_ENABLED);
102 |
103 | root.addPreference(notifyPref);
104 | root.addPreference(soundPref);
105 | root.addPreference(vibratePref);
106 |
107 | // prefCat.addPreference(notifyPref);
108 | // prefCat.addPreference(soundPref);
109 | // prefCat.addPreference(vibratePref);
110 | // root.addPreference(prefCat);
111 |
112 | return root;
113 | }
114 |
115 | private void setPreferenceDependencies() {
116 | Preference soundPref = getPreferenceManager().findPreference(
117 | Constants.SETTINGS_SOUND_ENABLED);
118 | if (soundPref != null) {
119 | soundPref.setDependency(Constants.SETTINGS_NOTIFICATION_ENABLED);
120 | }
121 | Preference vibratePref = getPreferenceManager().findPreference(
122 | Constants.SETTINGS_VIBRATE_ENABLED);
123 | if (vibratePref != null) {
124 | vibratePref.setDependency(Constants.SETTINGS_NOTIFICATION_ENABLED);
125 | }
126 | }
127 |
128 | }
129 |
--------------------------------------------------------------------------------
/src/org/androidpn/client/Notifier.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010 Moduad Co., Ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.androidpn.client;
17 |
18 | import java.util.Random;
19 |
20 | import android.app.Notification;
21 | import android.app.NotificationManager;
22 | import android.app.PendingIntent;
23 | import android.content.Context;
24 | import android.content.Intent;
25 | import android.content.SharedPreferences;
26 | import android.util.Log;
27 | import android.widget.Toast;
28 |
29 | /**
30 | * This class is to notify the user of messages with NotificationManager.
31 | *
32 | * @author Sehwan Noh (devnoh@gmail.com)
33 | */
34 | public class Notifier {
35 |
36 | private static final String LOGTAG = LogUtil.makeLogTag(Notifier.class);
37 |
38 | private static final Random random = new Random(System.currentTimeMillis());
39 |
40 | private Context context;
41 |
42 | private SharedPreferences sharedPrefs;
43 |
44 | private NotificationManager notificationManager;
45 |
46 | public Notifier(Context context) {
47 | this.context = context;
48 | this.sharedPrefs = context.getSharedPreferences(
49 | Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
50 | this.notificationManager = (NotificationManager) context
51 | .getSystemService(Context.NOTIFICATION_SERVICE);
52 | }
53 |
54 | public void notify(String notificationId, String apiKey, String title,
55 | String message, String uri) {
56 | Log.d(LOGTAG, "notify()...");
57 |
58 | Log.d(LOGTAG, "notificationId=" + notificationId);
59 | Log.d(LOGTAG, "notificationApiKey=" + apiKey);
60 | Log.d(LOGTAG, "notificationTitle=" + title);
61 | Log.d(LOGTAG, "notificationMessage=" + message);
62 | Log.d(LOGTAG, "notificationUri=" + uri);
63 |
64 | if (isNotificationEnabled()) {
65 | // Show the toast
66 | if (isNotificationToastEnabled()) {
67 | Toast.makeText(context, message, Toast.LENGTH_LONG).show();
68 | }
69 |
70 | // Notification
71 | Notification notification = new Notification();
72 | notification.icon = getNotificationIcon();
73 | notification.defaults = Notification.DEFAULT_LIGHTS;
74 | if (isNotificationSoundEnabled()) {
75 | notification.defaults |= Notification.DEFAULT_SOUND;
76 | }
77 | if (isNotificationVibrateEnabled()) {
78 | notification.defaults |= Notification.DEFAULT_VIBRATE;
79 | }
80 | notification.flags |= Notification.FLAG_AUTO_CANCEL;
81 | notification.when = System.currentTimeMillis();
82 | notification.tickerText = message;
83 |
84 | // Intent intent;
85 | // if (uri != null
86 | // && uri.length() > 0
87 | // && (uri.startsWith("http:") || uri.startsWith("https:")
88 | // || uri.startsWith("tel:") || uri.startsWith("geo:"))) {
89 | // intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
90 | // } else {
91 | // String callbackActivityPackageName = sharedPrefs.getString(
92 | // Constants.CALLBACK_ACTIVITY_PACKAGE_NAME, "");
93 | // String callbackActivityClassName = sharedPrefs.getString(
94 | // Constants.CALLBACK_ACTIVITY_CLASS_NAME, "");
95 | // intent = new Intent().setClassName(callbackActivityPackageName,
96 | // callbackActivityClassName);
97 | // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
98 | // intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
99 | // }
100 |
101 | Intent intent = new Intent(context,
102 | NotificationDetailsActivity.class);
103 | intent.putExtra(Constants.NOTIFICATION_ID, notificationId);
104 | intent.putExtra(Constants.NOTIFICATION_API_KEY, apiKey);
105 | intent.putExtra(Constants.NOTIFICATION_TITLE, title);
106 | intent.putExtra(Constants.NOTIFICATION_MESSAGE, message);
107 | intent.putExtra(Constants.NOTIFICATION_URI, uri);
108 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
109 | intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
110 | intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
111 | intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
112 | intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
113 |
114 | PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
115 | intent, PendingIntent.FLAG_UPDATE_CURRENT);
116 |
117 | notification.setLatestEventInfo(context, title, message,
118 | contentIntent);
119 | notificationManager.notify(random.nextInt(), notification);
120 |
121 | // Intent clickIntent = new Intent(
122 | // Constants.ACTION_NOTIFICATION_CLICKED);
123 | // clickIntent.putExtra(Constants.NOTIFICATION_ID, notificationId);
124 | // clickIntent.putExtra(Constants.NOTIFICATION_API_KEY, apiKey);
125 | // clickIntent.putExtra(Constants.NOTIFICATION_TITLE, title);
126 | // clickIntent.putExtra(Constants.NOTIFICATION_MESSAGE, message);
127 | // clickIntent.putExtra(Constants.NOTIFICATION_URI, uri);
128 | // // positiveIntent.setData(Uri.parse((new StringBuilder(
129 | // // "notif://notification.adroidpn.org/")).append(apiKey).append(
130 | // // "/").append(System.currentTimeMillis()).toString()));
131 | // PendingIntent clickPendingIntent = PendingIntent.getBroadcast(
132 | // context, 0, clickIntent, 0);
133 | //
134 | // notification.setLatestEventInfo(context, title, message,
135 | // clickPendingIntent);
136 | //
137 | // Intent clearIntent = new Intent(
138 | // Constants.ACTION_NOTIFICATION_CLEARED);
139 | // clearIntent.putExtra(Constants.NOTIFICATION_ID, notificationId);
140 | // clearIntent.putExtra(Constants.NOTIFICATION_API_KEY, apiKey);
141 | // // negativeIntent.setData(Uri.parse((new StringBuilder(
142 | // // "notif://notification.adroidpn.org/")).append(apiKey).append(
143 | // // "/").append(System.currentTimeMillis()).toString()));
144 | // PendingIntent clearPendingIntent = PendingIntent.getBroadcast(
145 | // context, 0, clearIntent, 0);
146 | // notification.deleteIntent = clearPendingIntent;
147 | //
148 | // notificationManager.notify(random.nextInt(), notification);
149 |
150 | } else {
151 | Log.w(LOGTAG, "Notificaitons disabled.");
152 | }
153 | }
154 |
155 | private int getNotificationIcon() {
156 | return sharedPrefs.getInt(Constants.NOTIFICATION_ICON, 0);
157 | }
158 |
159 | private boolean isNotificationEnabled() {
160 | return sharedPrefs.getBoolean(Constants.SETTINGS_NOTIFICATION_ENABLED,
161 | true);
162 | }
163 |
164 | private boolean isNotificationSoundEnabled() {
165 | return sharedPrefs.getBoolean(Constants.SETTINGS_SOUND_ENABLED, true);
166 | }
167 |
168 | private boolean isNotificationVibrateEnabled() {
169 | return sharedPrefs.getBoolean(Constants.SETTINGS_VIBRATE_ENABLED, true);
170 | }
171 |
172 | private boolean isNotificationToastEnabled() {
173 | return sharedPrefs.getBoolean(Constants.SETTINGS_TOAST_ENABLED, false);
174 | }
175 |
176 | }
177 |
--------------------------------------------------------------------------------
/src/org/androidpn/client/PersistentConnectionListener.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010 Moduad Co., Ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.androidpn.client;
17 |
18 | import org.jivesoftware.smack.ConnectionListener;
19 |
20 | import android.util.Log;
21 |
22 | /**
23 | * A listener class for monitoring connection closing and reconnection events.
24 | * 监听连接出错、中断等
25 | * @author Sehwan Noh (devnoh@gmail.com)
26 | */
27 | public class PersistentConnectionListener implements ConnectionListener {
28 |
29 | private static final String LOGTAG = LogUtil
30 | .makeLogTag(PersistentConnectionListener.class);
31 |
32 | private final XmppManager xmppManager;
33 |
34 | public PersistentConnectionListener(XmppManager xmppManager) {
35 | this.xmppManager = xmppManager;
36 | }
37 |
38 | @Override
39 | public void connectionClosed() {
40 | Log.d(LOGTAG, "connectionClosed()...");
41 | }
42 |
43 | @Override
44 | public void connectionClosedOnError(Exception e) {
45 | Log.d(LOGTAG, "connectionClosedOnError()...");
46 | if (xmppManager.getConnection() != null
47 | && xmppManager.getConnection().isConnected()) {
48 | xmppManager.getConnection().disconnect();
49 | }
50 | xmppManager.startReconnectionThread();
51 | }
52 |
53 | @Override
54 | public void reconnectingIn(int seconds) {
55 | Log.d(LOGTAG, "reconnectingIn()...");
56 | }
57 |
58 | @Override
59 | public void reconnectionFailed(Exception e) {
60 | Log.d(LOGTAG, "reconnectionFailed()...");
61 | }
62 |
63 | @Override
64 | public void reconnectionSuccessful() {
65 | Log.d(LOGTAG, "reconnectionSuccessful()...");
66 | }
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/src/org/androidpn/client/PhoneStateChangeListener.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010 Moduad Co., Ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.androidpn.client;
17 |
18 | import android.telephony.PhoneStateListener;
19 | import android.telephony.TelephonyManager;
20 | import android.util.Log;
21 |
22 | /**
23 | * A listener class for monitoring changes in phone connection states.
24 | *
25 | * @author Sehwan Noh (devnoh@gmail.com)
26 | */
27 | public class PhoneStateChangeListener extends PhoneStateListener {
28 |
29 | private static final String LOGTAG = LogUtil
30 | .makeLogTag(PhoneStateChangeListener.class);
31 |
32 | private final NotificationService notificationService;
33 |
34 | public PhoneStateChangeListener(NotificationService notificationService) {
35 | this.notificationService = notificationService;
36 | }
37 |
38 | @Override
39 | public void onDataConnectionStateChanged(int state) {
40 | super.onDataConnectionStateChanged(state);
41 | Log.d(LOGTAG, "onDataConnectionStateChanged()...");
42 | Log.d(LOGTAG, "Data Connection State = " + getState(state));
43 |
44 | if (state == TelephonyManager.DATA_CONNECTED) {
45 | notificationService.connect(); //这里又连接了一次服务器
46 | }
47 | }
48 |
49 | private String getState(int state) {
50 | switch (state) {
51 | case 0: // '\0'
52 | return "DATA_DISCONNECTED";
53 | case 1: // '\001'
54 | return "DATA_CONNECTING";
55 | case 2: // '\002'
56 | return "DATA_CONNECTED";
57 | case 3: // '\003'
58 | return "DATA_SUSPENDED";
59 | }
60 | return "DATA_";
61 | }
62 |
63 | }
64 |
--------------------------------------------------------------------------------
/src/org/androidpn/client/ReconnectionThread.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010 Moduad Co., Ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.androidpn.client;
17 |
18 | import android.util.Log;
19 |
20 | /**
21 | * A thread class for recennecting the server.
22 | *重新连接的线程
23 | * @author Sehwan Noh (devnoh@gmail.com)
24 | */
25 | public class ReconnectionThread extends Thread {
26 |
27 | private static final String LOGTAG = LogUtil
28 | .makeLogTag(ReconnectionThread.class);
29 |
30 | private final XmppManager xmppManager;
31 |
32 | private int waiting;
33 |
34 | ReconnectionThread(XmppManager xmppManager) {
35 | this.xmppManager = xmppManager;
36 | this.waiting = 0;
37 | }
38 |
39 | public void run() {
40 | try {
41 | while (!isInterrupted()) {
42 | Log.d(LOGTAG, "Trying to reconnect in " + waiting()
43 | + " seconds");
44 | Thread.sleep((long) waiting() * 1000L);
45 | xmppManager.connect();
46 | waiting++;
47 | }
48 | } catch (final InterruptedException e) {
49 | xmppManager.getHandler().post(new Runnable() {
50 | public void run() {
51 | xmppManager.getConnectionListener().reconnectionFailed(e);
52 | }
53 | });
54 | }
55 | }
56 |
57 | private int waiting() {
58 | if (waiting > 20) {
59 | return 600;
60 | }
61 | if (waiting > 13) {
62 | return 300;
63 | }
64 | return waiting <= 7 ? 10 : 60;
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/src/org/androidpn/client/ServiceManager.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010 Moduad Co., Ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.androidpn.client;
17 |
18 | import java.util.Properties;
19 |
20 | import android.app.Activity;
21 | import android.content.Context;
22 | import android.content.Intent;
23 | import android.content.SharedPreferences;
24 | import android.content.SharedPreferences.Editor;
25 | import android.util.Log;
26 |
27 | /**
28 | * This class is to manage the notificatin service and to load the configuration.
29 | * 管理NotificationService和加载配置(raw/androidpn.properties),并存入共享引用
30 | * @author Sehwan Noh (devnoh@gmail.com)
31 | */
32 | public final class ServiceManager {
33 |
34 | private static final String LOGTAG = LogUtil
35 | .makeLogTag(ServiceManager.class);
36 |
37 | private Context context;
38 |
39 | private SharedPreferences sharedPrefs;
40 | //加载raw文件夹下的属性文件
41 | private Properties props;
42 |
43 | private String version = "0.5.0";
44 |
45 | private String apiKey;
46 |
47 | private String xmppHost;
48 |
49 | private String xmppPort;
50 |
51 | private String callbackActivityPackageName;
52 |
53 | private String callbackActivityClassName;
54 |
55 | /**
56 | * ServiceManager的构造函数
57 | * @param context
58 | */
59 | public ServiceManager(Context context) {
60 | this.context = context;
61 |
62 | if (context instanceof Activity) {
63 | Log.i(LOGTAG, "Callback Activity...");
64 | Activity callbackActivity = (Activity) context;
65 | callbackActivityPackageName = callbackActivity.getPackageName();
66 | callbackActivityClassName = callbackActivity.getClass().getName();
67 | }
68 |
69 | // apiKey = getMetaDataValue("ANDROIDPN_API_KEY");
70 | // Log.i(LOGTAG, "apiKey=" + apiKey);
71 | // // if (apiKey == null) {
72 | // // Log.e(LOGTAG, "Please set the androidpn api key in the manifest file.");
73 | // // throw new RuntimeException();
74 | // // }
75 | //从属性文件中读取ApiKey,服务器地址和端口,并存入共享文件
76 |
77 |
78 |
79 |
80 |
81 | props = loadProperties();
82 | apiKey = props.getProperty("apiKey", "");
83 | xmppHost = props.getProperty("xmppHost", "127.0.0.1");
84 | xmppPort = props.getProperty("xmppPort", "5222");
85 | Log.i(LOGTAG, "apiKey=" + apiKey);
86 | Log.i(LOGTAG, "xmppHost=" + xmppHost);
87 | Log.i(LOGTAG, "xmppPort=" + xmppPort);
88 |
89 |
90 | sharedPrefs = context.getSharedPreferences(
91 | Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
92 | Editor editor = sharedPrefs.edit();
93 | editor.putString(Constants.API_KEY, apiKey);
94 | editor.putString(Constants.VERSION, version);
95 | editor.putString(Constants.XMPP_HOST, xmppHost);
96 | editor.putInt(Constants.XMPP_PORT, Integer.parseInt(xmppPort));
97 | editor.putString(Constants.CALLBACK_ACTIVITY_PACKAGE_NAME,
98 | callbackActivityPackageName);
99 | editor.putString(Constants.CALLBACK_ACTIVITY_CLASS_NAME,
100 | callbackActivityClassName);
101 | editor.commit();
102 | // Log.i(LOGTAG, "sharedPrefs=" + sharedPrefs.toString());
103 | }
104 |
105 | /**
106 | * 启动NotificationService
107 | */
108 | public void startService() {
109 | Thread serviceThread = new Thread(new Runnable() {
110 | @Override
111 | public void run() {
112 | Intent intent = NotificationService.getIntent();
113 | context.startService(intent);
114 | }
115 | });
116 | serviceThread.start();
117 | }
118 |
119 | /**
120 | * 关闭NotificationService
121 | */
122 | public void stopService() {
123 | Intent intent = NotificationService.getIntent();
124 | context.stopService(intent);
125 | }
126 |
127 | // private String getMetaDataValue(String name, String def) {
128 | // String value = getMetaDataValue(name);
129 | // return (value == null) ? def : value;
130 | // }
131 | //
132 | // private String getMetaDataValue(String name) {
133 | // Object value = null;
134 | // PackageManager packageManager = context.getPackageManager();
135 | // ApplicationInfo applicationInfo;
136 | // try {
137 | // applicationInfo = packageManager.getApplicationInfo(context
138 | // .getPackageName(), 128);
139 | // if (applicationInfo != null && applicationInfo.metaData != null) {
140 | // value = applicationInfo.metaData.get(name);
141 | // }
142 | // } catch (NameNotFoundException e) {
143 | // throw new RuntimeException(
144 | // "Could not read the name in the manifest file.", e);
145 | // }
146 | // if (value == null) {
147 | // throw new RuntimeException("The name '" + name
148 | // + "' is not defined in the manifest file's meta data.");
149 | // }
150 | // return value.toString();
151 | // }
152 |
153 | /**
154 | * 加载属性文件
155 | * @return
156 | */
157 | private Properties loadProperties() {
158 | // InputStream in = null;
159 | // Properties props = null;
160 | // try {
161 | // in = getClass().getResourceAsStream(
162 | // "/org/androidpn/client/client.properties");
163 | // if (in != null) {
164 | // props = new Properties();
165 | // props.load(in);
166 | // } else {
167 | // Log.e(LOGTAG, "Could not find the properties file.");
168 | // }
169 | // } catch (IOException e) {
170 | // Log.e(LOGTAG, "Could not find the properties file.", e);
171 | // } finally {
172 | // if (in != null)
173 | // try {
174 | // in.close();
175 | // } catch (Throwable ignore) {
176 | // }
177 | // }
178 | // return props;
179 |
180 | Properties props = new Properties();
181 | try {
182 | int id = context.getResources().getIdentifier("androidpn", "raw",
183 | context.getPackageName());
184 | props.load(context.getResources().openRawResource(id));
185 | } catch (Exception e) {
186 | Log.e(LOGTAG, "Could not find the properties file.", e);
187 | // e.printStackTrace();
188 | }
189 | return props;
190 | }
191 |
192 | // public String getVersion() {
193 | // return version;
194 | // }
195 | //
196 | // public String getApiKey() {
197 | // return apiKey;
198 | // }
199 |
200 | /**
201 | * 将通知的图标资源ID存入共享引用
202 | * @param iconId
203 | */
204 | public void setNotificationIcon(int iconId) {
205 | Editor editor = sharedPrefs.edit();
206 | editor.putInt(Constants.NOTIFICATION_ICON, iconId);
207 | editor.commit();
208 | }
209 |
210 | // public void viewNotificationSettings() {
211 | // Intent intent = new Intent().setClass(context,
212 | // NotificationSettingsActivity.class);
213 | // context.startActivity(intent);
214 | // }
215 |
216 | /**
217 | * 跳转到通知设置页面
218 | * @param context
219 | */
220 | public static void viewNotificationSettings(Context context) {
221 | Intent intent = new Intent().setClass(context,
222 | NotificationSettingsActivity.class);
223 | context.startActivity(intent);
224 | }
225 |
226 | }
227 |
--------------------------------------------------------------------------------
/src/org/androidpn/client/XmppManager.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010 Moduad Co., Ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.androidpn.client;
17 |
18 | import java.util.ArrayList;
19 | import java.util.List;
20 | import java.util.UUID;
21 | import java.util.concurrent.Future;
22 |
23 | import org.jivesoftware.smack.ConnectionConfiguration;
24 | import org.jivesoftware.smack.ConnectionListener;
25 | import org.jivesoftware.smack.PacketListener;
26 | import org.jivesoftware.smack.XMPPConnection;
27 | import org.jivesoftware.smack.XMPPException;
28 | import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
29 | import org.jivesoftware.smack.filter.AndFilter;
30 | import org.jivesoftware.smack.filter.PacketFilter;
31 | import org.jivesoftware.smack.filter.PacketIDFilter;
32 | import org.jivesoftware.smack.filter.PacketTypeFilter;
33 | import org.jivesoftware.smack.packet.IQ;
34 | import org.jivesoftware.smack.packet.Packet;
35 | import org.jivesoftware.smack.packet.Registration;
36 | import org.jivesoftware.smack.provider.ProviderManager;
37 |
38 | import android.content.Context;
39 | import android.content.SharedPreferences;
40 | import android.content.SharedPreferences.Editor;
41 | import android.os.Handler;
42 | import android.util.Log;
43 |
44 | /**
45 | * This class is to manage the XMPP connection between client and server.
46 | *
47 | * @author Sehwan Noh (devnoh@gmail.com)
48 | */
49 | public class XmppManager {
50 |
51 | private static final String LOGTAG = LogUtil.makeLogTag(XmppManager.class);
52 |
53 | private static final String XMPP_RESOURCE_NAME = "AndroidpnClient";
54 |
55 | private Context context;
56 |
57 | private NotificationService.TaskSubmitter taskSubmitter;
58 |
59 | private NotificationService.TaskTracker taskTracker;
60 |
61 | private SharedPreferences sharedPrefs;
62 |
63 | private String xmppHost;
64 |
65 | private int xmppPort;
66 |
67 | private XMPPConnection connection;
68 |
69 | private String username;
70 |
71 | private String password;
72 |
73 | private ConnectionListener connectionListener;
74 |
75 | private PacketListener notificationPacketListener;
76 |
77 | private Handler handler;
78 |
79 | private List taskList;
80 |
81 | private boolean running = false;
82 |
83 | private Future> futureTask;
84 |
85 | private Thread reconnection;
86 |
87 | /**
88 | * XmppManager的构造方法
89 | * 从共享引用中取得xmpp服务器地址和端口号、用户名和密码
90 | * 模拟器测试的话服务器地址设置为10.0.2.2
91 | * @param notificationService
92 | */
93 | public XmppManager(NotificationService notificationService) {
94 | context = notificationService;
95 | taskSubmitter = notificationService.getTaskSubmitter();
96 | taskTracker = notificationService.getTaskTracker();
97 | sharedPrefs = notificationService.getSharedPreferences();
98 |
99 | xmppHost = sharedPrefs.getString(Constants.XMPP_HOST, "localhost");
100 | xmppPort = sharedPrefs.getInt(Constants.XMPP_PORT, 5222);
101 | username = sharedPrefs.getString(Constants.XMPP_USERNAME, "");
102 | password = sharedPrefs.getString(Constants.XMPP_PASSWORD, "");
103 |
104 | connectionListener = new PersistentConnectionListener(this);
105 | notificationPacketListener = new NotificationPacketListener(this);
106 |
107 | handler = new Handler();
108 | taskList = new ArrayList();
109 | /**
110 | * 重连线程
111 | */
112 | reconnection = new ReconnectionThread(this);
113 | }
114 |
115 | public Context getContext() {
116 | return context;
117 | }
118 |
119 | public void connect() {
120 | Log.d(LOGTAG, "connect()...");
121 | submitLoginTask();
122 | }
123 |
124 | public void disconnect() {
125 | Log.d(LOGTAG, "disconnect()...");
126 | terminatePersistentConnection();
127 | }
128 |
129 | /**
130 | * 终止连接
131 | */
132 | public void terminatePersistentConnection() {
133 | Log.d(LOGTAG, "terminatePersistentConnection()...");
134 | Runnable runnable = new Runnable() {
135 |
136 | final XmppManager xmppManager = XmppManager.this;
137 |
138 | public void run() {
139 | if (xmppManager.isConnected()) {
140 | Log.d(LOGTAG, "terminatePersistentConnection()... run()");
141 | xmppManager.getConnection().removePacketListener(
142 | xmppManager.getNotificationPacketListener()); //移除数据包的监听
143 | xmppManager.getConnection().disconnect();
144 | }
145 | xmppManager.runTask();
146 | }
147 |
148 | };
149 | addTask(runnable);
150 | }
151 |
152 | public XMPPConnection getConnection() {
153 | return connection;
154 | }
155 |
156 | public void setConnection(XMPPConnection connection) {
157 | this.connection = connection;
158 | }
159 |
160 | public String getUsername() {
161 | return username;
162 | }
163 |
164 | public void setUsername(String username) {
165 | this.username = username;
166 | }
167 |
168 | public String getPassword() {
169 | return password;
170 | }
171 |
172 | public void setPassword(String password) {
173 | this.password = password;
174 | }
175 |
176 | public ConnectionListener getConnectionListener() {
177 | return connectionListener;
178 | }
179 |
180 | public PacketListener getNotificationPacketListener() {
181 | return notificationPacketListener;
182 | }
183 |
184 | /**
185 | * 启动重连线程
186 | */
187 | public void startReconnectionThread() {
188 | synchronized (reconnection) {
189 | if (!reconnection.isAlive()) {
190 | reconnection.setName("Xmpp Reconnection Thread");
191 | reconnection.start();
192 | }
193 | }
194 | }
195 |
196 | public Handler getHandler() {
197 | return handler;
198 | }
199 |
200 | public void reregisterAccount() {
201 | removeAccount();
202 | submitLoginTask();
203 | runTask();
204 | }
205 |
206 | public List getTaskList() {
207 | return taskList;
208 | }
209 |
210 | public Future> getFutureTask() {
211 | return futureTask;
212 | }
213 |
214 | /**
215 | * 执行队列中的任务
216 | * 执行该方法后 才真正执行run方法
217 | */
218 | public void runTask() {
219 | Log.d(LOGTAG, "runTask()...");
220 | synchronized (taskList) {
221 | running = false;
222 | futureTask = null;
223 | if (!taskList.isEmpty()) {
224 | Runnable runnable = (Runnable) taskList.get(0);
225 | taskList.remove(0);
226 | running = true;
227 | futureTask = taskSubmitter.submit(runnable);
228 | if (futureTask == null) {
229 | taskTracker.decrease();
230 | }
231 | }
232 | }
233 | taskTracker.decrease();
234 | Log.d(LOGTAG, "runTask()...done");
235 | }
236 |
237 | /**
238 | * 产生随机的UUID
239 | * @return
240 | */
241 | private String newRandomUUID() {
242 | String uuidRaw = UUID.randomUUID().toString();
243 | return uuidRaw.replaceAll("-", "");
244 | }
245 |
246 | /**
247 | *是否连接
248 | * @return
249 | */
250 | private boolean isConnected() {
251 | return connection != null && connection.isConnected();
252 | }
253 |
254 | /**
255 | * 是否登陆验证
256 | * @return
257 | */
258 | private boolean isAuthenticated() {
259 | return connection != null && connection.isConnected()
260 | && connection.isAuthenticated();
261 | }
262 |
263 | /**
264 | * 是否已注册
265 | * @return
266 | */
267 | private boolean isRegistered() {
268 | return sharedPrefs.contains(Constants.XMPP_USERNAME)
269 | && sharedPrefs.contains(Constants.XMPP_PASSWORD);
270 | }
271 |
272 | private void submitConnectTask() {
273 | Log.d(LOGTAG, "submitConnectTask()...");
274 | addTask(new ConnectTask());
275 | }
276 |
277 | private void submitRegisterTask() {
278 | Log.d(LOGTAG, "submitRegisterTask()...");
279 | submitConnectTask();
280 | addTask(new RegisterTask());
281 | }
282 |
283 | private void submitLoginTask() {
284 | Log.d(LOGTAG, "submitLoginTask()...");
285 | submitRegisterTask();
286 | addTask(new LoginTask());
287 | }
288 |
289 | /**
290 | * 添加任务到队列
291 | * @param runnable
292 | */
293 | private void addTask(Runnable runnable) {
294 | Log.d(LOGTAG, "addTask(runnable)... running: "+running);
295 | taskTracker.increase();
296 | synchronized (taskList) {
297 | if (taskList.isEmpty() && !running) {
298 | Log.d(LOGTAG, "addTask(runnable)... taskList.isEmpty() && !running");
299 | running = true;
300 | futureTask = taskSubmitter.submit(runnable);
301 | if (futureTask == null) {
302 | taskTracker.decrease();
303 | }
304 | } else {
305 | taskList.add(runnable);
306 | }
307 | }
308 | Log.d(LOGTAG, "addTask(runnable)... done");
309 | }
310 |
311 | private void removeAccount() {
312 | Editor editor = sharedPrefs.edit();
313 | editor.remove(Constants.XMPP_USERNAME);
314 | editor.remove(Constants.XMPP_PASSWORD);
315 | editor.commit();
316 | }
317 |
318 | /**
319 | * 执行顺序:第一步
320 | * A runnable task to connect the server.
321 | * 该内部类主要实现的是连接服务器的操作
322 | *
323 | * 通过connection.connect();实现连接服务器
324 | */
325 | private class ConnectTask implements Runnable {
326 |
327 | final XmppManager xmppManager;
328 |
329 | private ConnectTask() {
330 | this.xmppManager = XmppManager.this;
331 | }
332 |
333 | public void run() {
334 | Log.i(LOGTAG, "ConnectTask.run()...");
335 |
336 | if (!xmppManager.isConnected()) { //未连接到XMPP服务器
337 | // Create the configuration for this new connection
338 | /**
339 | * 设置连接的一些参数
340 | */
341 | ConnectionConfiguration connConfig = new ConnectionConfiguration(
342 | xmppHost, xmppPort);
343 | // connConfig.setSecurityMode(SecurityMode.disabled);
344 | connConfig.setSecurityMode(SecurityMode.required);
345 | connConfig.setSASLAuthenticationEnabled(false);
346 | connConfig.setCompressionEnabled(false);
347 |
348 | XMPPConnection connection = new XMPPConnection(connConfig);
349 | xmppManager.setConnection(connection);
350 |
351 | try {
352 | // Connect to the server
353 | connection.connect();
354 | Log.i(LOGTAG, "XMPP connected successfully");
355 |
356 | // packet provider
357 | /**
358 | * 这个就是对于通信的xml文本进行解析的解析器,再把信息转换成IQ,这个相当于QQ的聊天信息
359 | * 如果要用这个协议,其IQ的子类(NotificationIQ)
360 | * 和IQProvider的子类(NotificationIQProvider)要进行重写
361 | */
362 | ProviderManager.getInstance().addIQProvider("notification",
363 | "androidpn:iq:notification",
364 | new NotificationIQProvider());
365 |
366 | } catch (XMPPException e) {
367 | Log.e(LOGTAG, "XMPP connection failed", e);
368 | running = false;
369 | }
370 | //执行任务
371 | xmppManager.runTask();
372 |
373 | } else {
374 | Log.i(LOGTAG, "XMPP connected already");
375 | //执行任务
376 | xmppManager.runTask();
377 | }
378 | }
379 | }
380 |
381 | /**
382 | * 执行顺序:第二步
383 | * A runnable task to register a new user onto the server.
384 | * 该内部类主要实现注册一个新的用户在服务器
385 | */
386 | private class RegisterTask implements Runnable {
387 |
388 | final XmppManager xmppManager;
389 |
390 | private RegisterTask() {
391 | xmppManager = XmppManager.this;
392 | }
393 |
394 | public void run() {
395 | Log.i(LOGTAG, "RegisterTask.run()...");
396 |
397 | if (!xmppManager.isRegistered()) {
398 | final String newUsername = newRandomUUID();
399 | final String newPassword = newRandomUUID();
400 | //客户端发送到服务器注册的数据包,Packet的子类
401 | Registration registration = new Registration();
402 |
403 | /**
404 | * 数据包的ID和类型的过滤器
405 | */
406 | PacketFilter packetFilter = new AndFilter(new PacketIDFilter(
407 | registration.getPacketID()), new PacketTypeFilter(
408 | IQ.class));
409 | /**
410 | * 数据包的监听
411 | */
412 | PacketListener packetListener = new PacketListener() {
413 |
414 | public void processPacket(Packet packet) {
415 | Log.d("RegisterTask.PacketListener",
416 | "processPacket().....");
417 | Log.d("RegisterTask.PacketListener", "packet="
418 | + packet.toXML());
419 | //服务器回复客户端
420 | if (packet instanceof IQ) {
421 | IQ response = (IQ) packet;
422 | if (response.getType() == IQ.Type.ERROR) { //注册失败
423 | if (!response.getError().toString().contains(
424 | "409")) {
425 | Log.e(LOGTAG,
426 | "Unknown error while registering XMPP account! "
427 | + response.getError()
428 | .getCondition());
429 | }
430 | } else if (response.getType() == IQ.Type.RESULT) { //注册成功
431 | xmppManager.setUsername(newUsername);
432 | xmppManager.setPassword(newPassword);
433 | Log.d(LOGTAG, "username=" + newUsername);
434 | Log.d(LOGTAG, "password=" + newPassword);
435 | //把用户名和密码保存到共享引用
436 | Editor editor = sharedPrefs.edit();
437 | editor.putString(Constants.XMPP_USERNAME,
438 | newUsername);
439 | editor.putString(Constants.XMPP_PASSWORD,
440 | newPassword);
441 | editor.commit();
442 | Log.i(LOGTAG,
443 | "Account registered successfully");
444 | xmppManager.runTask();
445 | }
446 | }
447 | }
448 | };
449 | // 给注册的Packet设置Listener,因为只有等到正真注册成功后,我们才可以交流
450 | connection.addPacketListener(packetListener, packetFilter);
451 |
452 | registration.setType(IQ.Type.SET);
453 | // registration.setTo(xmppHost);
454 | // Map attributes = new HashMap();
455 | // attributes.put("username", rUsername);
456 | // attributes.put("password", rPassword);
457 | // registration.setAttributes(attributes);
458 | registration.addAttribute("username", newUsername);
459 | registration.addAttribute("password", newPassword);
460 |
461 | registration.addAttribute("imsi", "460000001232300");
462 | registration.addAttribute("imei", "324234343434434");
463 |
464 | // 向服务器端,发送注册Packet包,注意其中Registration是Packet的子类
465 | connection.sendPacket(registration);
466 |
467 | } else {
468 | Log.i(LOGTAG, "Account registered already");
469 | xmppManager.runTask();
470 | }
471 | }
472 | }
473 |
474 | /**
475 | * 执行顺序:第三步
476 | * A runnable task to log into the server.
477 | * 该内部类主要实现用注册的账户和密码进行登陆
478 | */
479 | private class LoginTask implements Runnable {
480 |
481 | final XmppManager xmppManager;
482 |
483 | private LoginTask() {
484 | this.xmppManager = XmppManager.this;
485 | }
486 |
487 | public void run() {
488 | Log.i(LOGTAG, "LoginTask.run()...");
489 | //判断是否已经登陆过了
490 | if (!xmppManager.isAuthenticated()) {
491 | Log.d(LOGTAG, "username=" + username);
492 | Log.d(LOGTAG, "password=" + password);
493 |
494 | try {
495 | xmppManager.getConnection().login(
496 | xmppManager.getUsername(),
497 | xmppManager.getPassword(), XMPP_RESOURCE_NAME);
498 | Log.d(LOGTAG, "Loggedn in successfully");
499 |
500 | // connection listener
501 | /**
502 | * 设置XmppConnection的监听器
503 | * 连接过程中有可能连接突然中断,连接出错等等问题,要进行监听
504 | */
505 | if (xmppManager.getConnectionListener() != null) {
506 | xmppManager.getConnection().addConnectionListener(
507 | xmppManager.getConnectionListener());
508 | }
509 |
510 | // packet filter
511 | /**
512 | * 数据包的过滤器,识别类型是NotificationIQ的数据包
513 | */
514 | PacketFilter packetFilter = new PacketTypeFilter(
515 | NotificationIQ.class);
516 | // packet listener
517 | /**
518 | * 设置服务器端推送信息数据包的监听器
519 | */
520 | PacketListener packetListener = xmppManager
521 | .getNotificationPacketListener();
522 | connection.addPacketListener(packetListener, packetFilter);
523 |
524 | xmppManager.runTask();
525 |
526 | } catch (XMPPException e) {
527 | Log.e(LOGTAG, "LoginTask.run()... xmpp error");
528 | Log.e(LOGTAG, "Failed to login to xmpp server. Caused by: "
529 | + e.getMessage());
530 | /**
531 | * 登陆失败,应该重试
532 | */
533 | String INVALID_CREDENTIALS_ERROR_CODE = "401";
534 | String errorMessage = e.getMessage();
535 | /**
536 | * 如果只是因为没有注册,则进行重新注册
537 | */
538 | if (errorMessage != null
539 | && errorMessage
540 | .contains(INVALID_CREDENTIALS_ERROR_CODE)) {
541 | xmppManager.reregisterAccount();
542 | return;
543 | }
544 | xmppManager.startReconnectionThread();
545 |
546 | } catch (Exception e) { // 有可能mConnection都为空
547 | Log.e(LOGTAG, "LoginTask.run()... other error");
548 | Log.e(LOGTAG, "Failed to login to xmpp server. Caused by: "
549 | + e.getMessage());
550 | xmppManager.startReconnectionThread(); //启动重连线程
551 | }
552 |
553 | } else { //已经登陆
554 | Log.i(LOGTAG, "Logged in already");
555 | xmppManager.runTask();
556 | }
557 |
558 | }
559 | }
560 |
561 | }
562 |
--------------------------------------------------------------------------------
/src/org/androidpn/demoapp/DemoAppActivity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010 Moduad Co., Ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.androidpn.demoapp;
17 |
18 | import org.androidpn.client.ServiceManager;
19 |
20 | import android.app.Activity;
21 | import android.os.Bundle;
22 | import android.util.Log;
23 | import android.view.View;
24 | import android.widget.Button;
25 |
26 | /**
27 | * This is an androidpn client demo application.
28 | *
29 | * @author Sehwan Noh (devnoh@gmail.com)
30 | */
31 | public class DemoAppActivity extends Activity {
32 |
33 | @Override
34 | public void onCreate(Bundle savedInstanceState) {
35 | Log.d("DemoAppActivity", "onCreate()...");
36 |
37 | super.onCreate(savedInstanceState);
38 | setContentView(R.layout.main);
39 |
40 | // Settings
41 | Button okButton = (Button) findViewById(R.id.btn_settings);
42 | okButton.setOnClickListener(new View.OnClickListener() {
43 | public void onClick(View view) {
44 | ServiceManager.viewNotificationSettings(DemoAppActivity.this);
45 | }
46 | });
47 |
48 | // Start the service
49 | ServiceManager serviceManager = new ServiceManager(this);
50 | serviceManager.setNotificationIcon(R.drawable.notification);
51 | serviceManager.startService();
52 | }
53 |
54 | }
--------------------------------------------------------------------------------