Commit 65707bcf authored by Luis Vidal's avatar Luis Vidal
Browse files

Initial checking of OpenWeatherMap weather provider service

- Moved the implementation from cLock to a new package
- Refactored the code so this service can function as a
  weather provider service and can be detected by the weather
  manager service

Change-Id: Id353451749dda940cecfd3e706e2662e233f0d2f
TICKET: CYNGNOS-2544
parent 98531e31
#
# Copyright (C) 2016 The CyanogenMod Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_PACKAGE_NAME := OpenWeatherMapProvider
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_MODULE_TAGS := optional
LOCAL_STATIC_JAVA_LIBRARIES := \
org.cyanogenmod.platform.sdk
include $(BUILD_PACKAGE)
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2012 The CyanogenMod Project (DvTonder)
Copyright (C) 2016 The CyanogenMod Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.cyanogenmod.openweathermapprovider" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:label="@string/app_name" android:allowBackup="true">
<service
android:name=".OpenWeatherMapProviderService"
android:label="@string/app_name"
android:exported="true"
android:permission="cyanogenmod.permission.BIND_WEATHER_PROVIDER_SERVICE">
<intent-filter>
<action android:name="cyanogenmod.weatherservice.WeatherProviderService" />
</intent-filter>
<meta-data
android:name="cyanogenmod.weatherservice"
android:resource="@xml/openweathermap" />
</service>
<activity android:name=".SettingsActivity"
android:label="@string/app_name"
android:exported="true" />
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2016 The CyanogenMod Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<resources>
<style name="AppTheme" parent="android:Theme.Material.Light.DarkActionBar">
</style>
</resources>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2016 The CyanogenMod Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2016 The CyanogenMod Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="app_name" translatable="false">OpenWeatherMap</string>
<string name="prefscreen_category_authentication_title">Settings</string>
<string name="prefscreen_category_about_title">About</string>
<!-- Authentication strings -->
<string name="prefscreen_api_key_title">API key</string>
<string name="prefscreen_api_key_summary">Your private API key to access %1$s weather data</string>
<string name="prefscreen_create_key_title">Create account</string>
<string name="prefscreen_create_key_summary">Create an account and generate your API key</string>
<string name="api_key_not_set_message">You must set a private API key to use %1$s</string>
<!-- Copyright and disclaimers -->
<string name="prefscreen_copyright_title">Disclaimer</string>
<string name="prefscreen_copyright_summary">The information provided by this application is provided for general information purposes only and does not claim to be or constitute legal or other professional advice and shall not be relied upon %1$s nor the provider of this application as such.</string>
<string name="openweathermap_inc_name" translatable="false">OpenWeatherMap, Inc.</string>
</resources>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2016 The CyanogenMod Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
</style>
</resources>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2016 The CyanogenMod Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<weather-provider-service xmlns:android="http://schemas.android.com/apk/res/android"
android:settingsActivity="org.cyanogenmod.openweathermapprovider.SettingsActivity"/>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2016 The CyanogenMod Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="@string/prefscreen_category_authentication_title">
<EditTextPreference
android:key="api_key"
android:title="@string/prefscreen_api_key_title"
android:summary="@string/prefscreen_api_key_summary"
android:dialogTitle="@string/prefscreen_api_key_title"
android:defaultValue="">
</EditTextPreference>
<PreferenceScreen
android:key="create_key"
android:title="@string/prefscreen_create_key_title"
android:summary="@string/prefscreen_create_key_summary">
<intent android:action="android.intent.action.VIEW"
android:data="https://home.openweathermap.org/users/sign_up" />
</PreferenceScreen>
</PreferenceCategory>
<PreferenceCategory android:title="@string/prefscreen_category_about_title">
<Preference
android:key="copyright"
android:title="@string/prefscreen_copyright_title"
android:summary="@string/prefscreen_copyright_summary"/>
</PreferenceCategory>
</PreferenceScreen>
\ No newline at end of file
/*
* Copyright (C) 2016 The CyanogenMod Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.cyanogenmod.openweathermapprovider;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class HttpRetriever {
public static String retrieve(String url) {
URL targetURL;
try {
targetURL = new URL(url);
} catch (MalformedURLException e) {
return null;
}
HttpURLConnection urlConnection = null;
String response;
try {
urlConnection = (HttpURLConnection) targetURL.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoInput(true);
urlConnection.connect();
response = readStream(urlConnection.getInputStream());
} catch (IOException e) {
return null;
} finally {
if (urlConnection != null) urlConnection.disconnect();
}
return response;
}
private static String readStream(InputStream inputStream) {
BufferedReader br;
br = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder builder = new StringBuilder();
String line;
try {
while ((line = br.readLine()) != null) {
builder.append(line);
}
} catch (IOException e) {
//Unable to read from the stream
return null;
}
return builder.toString();
}
}
/*
* Copyright (C) 2016 The CyanogenMod Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.cyanogenmod.openweathermapprovider;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.widget.Toast;
public class SettingsActivity extends Activity {
private final static String API_KEY = "api_key";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new ServicePrefsFragment())
.commit();
}
public static class ServicePrefsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
addPreferencesFromResource(R.xml.preferences);
//Format some strings with arguments
EditTextPreference apiKey = (EditTextPreference)findPreference("api_key");
apiKey.setSummary(getString(R.string.prefscreen_api_key_summary,
getString(R.string.app_name)));
Preference copyright = findPreference("copyright");
copyright.setSummary(getString(R.string.prefscreen_copyright_summary,
getString(R.string.openweathermap_inc_name)));
}
@Override
public void onResume() {
super.onResume();
Context context = getActivity();
if (context != null) {
String apiKey = getPreferenceManager().getSharedPreferences()
.getString(API_KEY, null);
if (apiKey == null || apiKey.equals("")) {
Toast.makeText(context, getString(R.string.api_key_not_set_message,
getString(R.string.app_name)), Toast.LENGTH_LONG).show();
}
}
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment