Skip to content

Commit a40f347

Browse files
author
Michael Black
committed
Updated old Diagnostic plugin for PhoneGap >= 3
1 parent 86d5389 commit a40f347

File tree

5 files changed

+509
-0
lines changed

5 files changed

+509
-0
lines changed

plugin.xml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
id="cordova.plugins.diagnostic"
5+
version="0.0.1">
6+
7+
<name>Diagnostic</name>
8+
<description>The diagnostic plugin allows you to check different device settings in your PhoneGap application.</description>
9+
10+
<js-module src="www/diagnostic.js" name="Diagnostic">
11+
<clobbers target="cordova.plugins.diagnostic" />
12+
</js-module>
13+
14+
<!-- ios -->
15+
<platform name="ios">
16+
<config-file target="config.xml" parent="/*">
17+
<feature name="Diagnostic">
18+
<param name="ios-package" value="Diagnostic" />
19+
</feature>
20+
</config-file>
21+
22+
<header-file src="src/ios/Diagnostic.h" />
23+
<source-file src="src/ios/Diagnostic.m" />
24+
25+
<framework src="CoreLocation.framework" />
26+
</platform>
27+
28+
<!-- android -->
29+
<platform name="android">
30+
<config-file target="config.xml" parent="/*">
31+
<feature name="Diagnostic" >
32+
<param name="android-package" value="com.collectme.cordova.diagnostic.Diagnostic"/>
33+
</feature>
34+
</config-file>
35+
36+
<config-file target="AndroidManifest.xml" parent="/manifest">
37+
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
38+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
39+
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
40+
</config-file>
41+
42+
<source-file src="src/android/Diagnostic.java" target-dir="src/com/collectme/cordova/diagnostic" />
43+
44+
</platform>
45+
</plugin>

src/android/Diagnostic.java

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
*/
19+
package com.collectme.cordova.diagnostic;
20+
21+
import java.util.TimeZone;
22+
23+
import org.apache.cordova.CordovaWebView;
24+
import org.apache.cordova.CallbackContext;
25+
import org.apache.cordova.CordovaPlugin;
26+
import org.apache.cordova.LOG;
27+
import org.apache.cordova.CordovaInterface;
28+
import org.json.JSONArray;
29+
import org.json.JSONException;
30+
import org.json.JSONObject;
31+
import android.util.Log;
32+
33+
import android.content.BroadcastReceiver;
34+
import android.content.Context;
35+
import android.content.Intent;
36+
import android.content.IntentFilter;
37+
import android.content.pm.PackageManager;
38+
import android.provider.Settings;
39+
import android.location.LocationManager;
40+
import android.location.LocationListener;
41+
import android.net.wifi.WifiManager;
42+
43+
public class Diagnostic extends CordovaPlugin {
44+
public static final String TAG = "Diagnostic";
45+
46+
/**
47+
* Constructor.
48+
*/
49+
public Diagnostic() {
50+
}
51+
52+
/**
53+
* Sets the context of the Command. This can then be used to do things like
54+
* get file paths associated with the Activity.
55+
*
56+
* @param cordova The context of the main Activity.
57+
* @param webView The CordovaWebView Cordova is running in.
58+
*/
59+
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
60+
super.initialize(cordova, webView);
61+
}
62+
63+
/**
64+
* Executes the request and returns PluginResult.
65+
*
66+
* @param action The action to execute.
67+
* @param args JSONArry of arguments for the plugin.
68+
* @param callbackContext The callback id used when calling back into JavaScript.
69+
* @return True if the action was valid, false if not.
70+
*/
71+
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
72+
JSONObject r = new JSONObject();
73+
74+
if (action.equals("switchToLocationSettings")){
75+
switchToLocationSettings();
76+
callbackContext.success();
77+
} else if(action.equals("isLocationEnabled")) {
78+
r.put("success", isGpsEnabled() || isNetworkEnabled());
79+
callbackContext.success(r);
80+
} else if(action.equals("isLocationAuthorized") || action.equals("isLocationEnabledSetting")) {
81+
r.put("success", true);
82+
callbackContext.success(r);
83+
} else if(action.equals("isWifiEnabled")) {
84+
r.put("success", isWifiEnabled());
85+
callbackContext.success(r);
86+
} else if(action.equals("isCameraEnabled")) {
87+
r.put("success", isCameraEnabled());
88+
callbackContext.success(r);
89+
}
90+
else {
91+
return false;
92+
}
93+
return true;
94+
}
95+
96+
/**
97+
* Check device settings for GPS.
98+
*
99+
* @returns {boolean} The status of GPS in device settings.
100+
*/
101+
public boolean isGpsEnabled() {
102+
boolean result = isLocationProviderEnabled(LocationManager.GPS_PROVIDER);
103+
Log.d(TAG, "GPS enabled: " + result);
104+
return result;
105+
}
106+
107+
public boolean isNetworkEnabled() {
108+
boolean result = isLocationProviderEnabled(LocationManager.NETWORK_PROVIDER);
109+
Log.d(TAG, "Network enabled: " + result);
110+
return result;
111+
}
112+
113+
public boolean isWifiEnabled() {
114+
WifiManager wifiManager = (WifiManager) this.cordova.getActivity().getSystemService(Context.WIFI_SERVICE);
115+
boolean result = wifiManager.isWifiEnabled();
116+
return result;
117+
}
118+
119+
public boolean isCameraEnabled() {
120+
PackageManager pm = this.cordova.getActivity().getPackageManager();
121+
boolean result = pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
122+
return result;
123+
}
124+
125+
/**
126+
* Requests that the user enable the location in device settings.
127+
*/
128+
public void switchToLocationSettings() {
129+
Log.d(TAG, "Switch to Location Settings");
130+
Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
131+
cordova.getActivity().startActivity(settingsIntent);
132+
}
133+
134+
private boolean isLocationProviderEnabled(String provider) {
135+
LocationManager locationManager = (LocationManager) this.cordova.getActivity().getSystemService(Context.LOCATION_SERVICE);
136+
return locationManager.isProviderEnabled(provider);
137+
}
138+
139+
}

src/ios/Diagnostic.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// Diagnostic.h
3+
// Plugin diagnostic
4+
//
5+
// Copyright (c) 2012 AVANTIC ESTUDIO DE INGENIEROS
6+
//
7+
8+
#import <Cordova/CDV.h>
9+
10+
@interface Diagnostic : CDVPlugin
11+
12+
- (void) isLocationEnabled: (CDVInvokedUrlCommand*)command;
13+
- (void) isLocationEnabledSetting: (CDVInvokedUrlCommand*)command;
14+
- (void) switchToLocationSettings: (CDVInvokedUrlCommand*)command;
15+
- (void) isLocationAuthorized: (CDVInvokedUrlCommand*)command;
16+
- (void) isWifiEnabled: (CDVInvokedUrlCommand*)command;
17+
- (void) isCameraEnabled: (CDVInvokedUrlCommand*)command;
18+
19+
@end

0 commit comments

Comments
 (0)