Start Chrome as Web-App on Android start - android

i have a quite specific problem. I have realized a Web App on an Android tablet, which will be used on an exhibition (Outform iDisplay). For this reason, the Web App has to start directly after boot. The after-boot thing is no problem (Broadcast with "android.permission.RECEIVE_BOOT_COMPLETED"), but i have a problem to start Chrome as Web-App. For getting the Intent, i have read the Icons in the launcher favorites with this snippet:
//Kitkat, therefore launcher3
url = "content://com.android.launcher3.settings/favorites?Notify=true";
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(Uri.parse(url), null, null, null, null);
if (cursor != null && cursor.moveToFirst())
{
do
{
String ent1 = cursor.getString(0);
String ent2 = cursor.getString(1);
String ent3 = cursor.getString(2); //there is the Intent string
String ent4 = cursor.getString(3);
System.out.println("Test");
String ent5 = cursor.getString(4);
String ent6 = cursor.getString(5);
String ent7 = cursor.getString(6);
String ent8 = cursor.getString(7);
String ent9 = cursor.getString(8);
String ent10 = cursor.getString(9);
String ent11 = cursor.getString(10);
String ent12 = cursor.getString(11);
String ent14 = cursor.getString(13);
String ent15 = cursor.getString(14);
String ent17 = cursor.getString(16);
String ent18 = cursor.getString(17);
String ent19 = cursor.getString(18);
String ent20 = cursor.getString(19);
if(ent2.equals("History Book")) //Get the right intent
{
runAction = ent3;
}
System.out.println(ent3);
} while (cursor.moveToNext());
}
The Intent string contains something like this:
#Intent;action=com.google.android.apps.chrome.webapps.WebappManager.ACTION_START_WEBAPP;package=com.android.chrome;S.org.chromium.chrome.browser.webapp_title=History%20Book;S.org.chromium.chrome.browser.webapp_id=86e362e4-a25d-4142-8a32-c02ffcb176a9;i.org.chromium.content_public.common.orientation=6;S.org.chromium.chrome.browser.webapp_icon=;S.org.chromium.chrome.browser.webapp_mac=3ZaXFbyWnJQaqFFOuUj3OssNz7DrBaaiWfzO2Dd7VIU%3D%0A;S.org.chromium.chrome.browser.webapp_url=http%3A%2F%2F192.168.5.148%2Fstyria%2Fhistorybook%2Findex.html;end
This looks quite good, but how can i start an Intent like this in a small app, which just has the single purpose to start this intent?
Just a small note at the end: I have tried to pack this thing into a webview, but the webview died constantly because of an libc error, so this is no option for me.

Finally i got this thing working. I was on the right way, but some Chrome.apk reverse engineering helped me for the last mile.
I have created a dummy activity with the following code in onCreate:
Search for the right entry on the homescreen, in my case for the AOSP launcher 3:
//Search for the History Book Shortcut on the Homescreen
String url = "";
String runAction="";
final String AUTHORITY = "com.android.launcher3.settings";
final Uri CONTENT_URI = Uri.parse("content://" +
AUTHORITY + "/favorites?notify=true");
final ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(CONTENT_URI,null,null,null,null);
cursor.moveToFirst();
do {
String id = cursor.getString(cursor.getColumnIndex("_id"));
String title = cursor.getString(cursor.getColumnIndex("title"));
String intent = cursor.getString(cursor.getColumnIndex("intent"));
if(title.equals(getResources().getString(R.string.homescreen_link)))
{
runAction = intent;
}
} while (cursor.moveToNext());
At this point, i have hopefully the intent as string. So, parse the string and create a new intent:
Intent intent = new Intent();
intent.setAction("com.google.android.apps.chrome.webapps.WebappManager.ACTION_START_WEBAPP");
intent.setPackage("com.android.chrome");
intent.setClassName("com.android.chrome", "com.google.android.apps.chrome.webapps.WebappManager");
HashMap<String, String> intentVals = getIntentParams(runAction);
intent.putExtra("org.chromium.chrome.browser.webapp_title",intentVals.get("S.org.chromium.chrome.browser.webapp_title"));
intent.putExtra("org.chromium.chrome.browser.webapp_icon",intentVals.get("S.org.chromium.chrome.browser.webapp_icon"));
intent.putExtra("org.chromium.chrome.browser.webapp_id",intentVals.get("S.org.chromium.chrome.browser.webapp_id"));
intent.putExtra("org.chromium.chrome.browser.webapp_url",intentVals.get("S.org.chromium.chrome.browser.webapp_url"));
intent.putExtra("org.chromium.chrome.browser.webapp_mac",intentVals.get("S.org.chromium.chrome.browser.webapp_mac"));
int orientation = 6;
try
{
orientation = Integer.parseInt(intentVals.get("i.org.chromium.content_public.common.orientation"));
}
catch(NumberFormatException _nex)
{
Log.e(TAG, "Wrong format, using default (6)");
}
intent.putExtra("org.chromium.content_public.common.orientation", orientation);
try
{
byte[] abyte0 = Base64.decode(
intentVals.get("S.org.chromium.chrome.browser.webapp_mac"),
0);
System.out.println(new String(abyte0));
}
catch (IllegalArgumentException _iae)
{
Log.e(TAG,
"Wrong webapp_mac: "
+ intentVals
.get("S.org.chromium.chrome.browser.webapp_mac"));
}
startActivity(intent);
finish();
And this function parses the intent parameters out of the intent string:
private HashMap<String, String> getIntentParams(String _runAction)
{
HashMap<String, String> retMap = new HashMap<String, String>();
String[] pairs = _runAction.split(";");
for (int i = 0; i < pairs.length; i++)
{
String[] keyval = pairs[i].split("=");
if(keyval.length==2)
{
String key = keyval[0];
String value = "";
try
{
value = java.net.URLDecoder.decode(keyval[1], "UTF-8");
}
catch (UnsupportedEncodingException _uee)
{
Log.e(TAG, "Unsupported Encoding: " + _uee.getMessage());
}
retMap.put(key, value);
}
}
return retMap;
}
And the strings.xml in res/values:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">WebAppStarter</string>
<string name="homescreen_link">History Book</string>
</resources>
That's it. You can configure the Homescreen link name to search for in strings.xml. When the app finds the string, it parses the intent string and creates a new intent to start Chrome as a Full Screen Activity Web App.

Related

how to use helper class to send and receive data using intent in android

enter image description here
I want to fetch the data from previous activity (using getIntent) in a .java class and same the data to next activity through intent. can anyone help me how.Thanks in advance.
public class AddToCartHelper {
public static void addToCart(Context context, Intent intent) {
String TAG = "AddToCart";
DecimalFormat decimalFormat;
boolean loginflagforuser = false, loginflagforguest = false;
String advId = "", num = "", uid = "", productName = "", emailCart = "",cartMessage = "";
Double price = 0.0;
Integer quantity = 1;
SharedPreferences preferences = context.getSharedPreferences("SECRETFILE", Context.MODE_PRIVATE);
loginflagforuser = preferences.getBoolean(Parameters.userEmail, false);
loginflagforguest = preferences.getBoolean(Parameters.guestEmail, false);
decimalFormat = new DecimalFormat("##.##");
if (loginflagforuser){
Intent fromCart = getIntent();
// imageId = fromCart.getStringExtra("image_url");
advId = fromCart.getStringExtra("Advid");
price = fromCart.getDoubleExtra("price", 0.0);
num = fromCart.getStringExtra("num");
uid = fromCart.getStringExtra("uid");
Log.d(TAG, "--- REGISTERD UID::::::::: " + uid);
quantity = fromCart.getIntExtra("quantity", 1);
productName = fromCart.getStringExtra("cart_product_name");
// total = fromCart.getDoubleExtra("total", 0.0);
emailCart = preferences.getString("email", null);
}else if (loginflagforguest){
}else{
}
}
}
you don't need to use this Intent fromCart = getIntent();,because in constructor you already pass intent ,then just use intent object
dvId = intent.getStringExtra("Advid");
price = intent.getDoubleExtra("price", 0.0);
num = intent.getStringExtra("num");
uid = intent.getStringExtra("uid");
Log.d(TAG, "--- REGISTERD UID::::::::: " + uid);
quantity = intent.getIntExtra("quantity", 1);
productName = intent.getStringExtra("cart_product_name");
// total = intent.getDoubleExtra("total", 0.0);
You have already pass parameter intent to addToCart function, so instead of advId = fromCart.getStringExtra("Advid");, you can use advId = intent.getStringExtra("Advid");
there is two ways to passing the data to the next activity you can either use intent or can use local broadcast receiver
if you want to fetch data from the previous activity then use
String a= getIntent().getStringExtra( "");// pass the name that you used in the previous activity

Auto Reply to WhatsApp message in background

I just checking out the Application, is doing auto reply to WhatsApp message in background. I also trying to doing so, but can't get success in it.
I had tried :
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);
But it opening the WhatsApp application :(, not sending the message.
I go through with several links: Question1, Question2 also article on it but not getting satisfactory answer.
The application are accessing the notifications to get the messages, and replying to it, I also tried reading notifications using NotificationListenerService, and got success to read message :), but can't send reply to it, I want to know how they are sending the messages in the background without opening the application.
I haven't tested this but I think this can be done via
Read Notification Bar title, message using Accessibility Service Programmatically
and https://developer.android.com/reference/android/app/RemoteInput
From doc :
public static final String KEY_QUICK_REPLY_TEXT = "quick_reply";
Notification.Action action = new Notification.Action.Builder(
R.drawable.reply, "Reply", actionIntent)
.addRemoteInput(new RemoteInput.Builder(KEY_QUICK_REPLY_TEXT)
.setLabel("Quick reply").build())
.build();
On a rooted device you can simply insert a message into the database
/data/data/com.whatsapp/databases/msgstore.db like this.
Firstly get the contacts from the database. WhatsApp uses its own IDs for contacts(not user numbers) in the jid column, will have to get that and the display name.
class Contact{
public String jid;
public String displayName;
public Contact(String displayName,String jid){
this.displayName = displayName;
this.jid = jid;
}
}
public List<Contact> getContacts(){
Shell.SU.run("am force-stop com.whatsapp");
Shell.SU.run("chmod 777 /data/data/com.whatsapp");
db = SQLiteDatabase.openOrCreateDatabase(new File("/data/data/com.whatsapp/databases/wa.db"), null);
List<Contact> contactList = new LinkedList<>();
String selectQuery = "SELECT jid, display_name FROM wa_contacts where phone_type is not null and is_whatsapp_user = 1";
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact(cursor.getString(1), cursor.getString(0));
contactList.add(contact);
} while (cursor.moveToNext());
}
db.close();
}
Then send the message like so
private void sendBigMessage(String jid, String msg, String file, String mimeType) {
Shell.SU.run("am force-stop com.whatsapp");
db = SQLiteDatabase.openOrCreateDatabase(new File("/data/data/com.whatsapp/databases/msgstore.db"), null);
long l1;
long l2;
int k;
String query2, query1;
Random localRandom = new Random(20L);
l1 = System.currentTimeMillis();
l2 = l1 / 1000L;
k = localRandom.nextInt();
int mediaType = 0;
if (mimeType == null || mimeType.length() < 2)
mediaType = 0;
else
mediaType = (mimeType.contains("video")) ? 3
: (mimeType.contains("image")) ? 1
: (mimeType.contains("audio")) ? 2
: 0;
ContentValues initialValues = new ContentValues();
initialValues.put("key_remote_jid", jid);
initialValues.put("key_from_me", 1);
initialValues.put("key_id", l2 + "-" + k);
initialValues.put("status", 1);
initialValues.put("needs_push", 0);
initialValues.put("timestamp", l1);
initialValues.put("media_wa_type", mediaType);
initialValues.put("media_name", file);
initialValues.put("latitude", 0.0);
initialValues.put("longitude", 0.0);
initialValues.put("received_timestamp", l1);
initialValues.put("send_timestamp", -1);
initialValues.put("receipt_server_timestamp", -1);
initialValues.put("receipt_device_timestamp", -1);
initialValues.put("raw_data", -1);
initialValues.put("recipient_count", 0);
initialValues.put("media_duration", 0);
if (!TextUtils.isEmpty(file) && !TextUtils.isEmpty(mimeType)) {
//boolean isVideo = mimeType.contains("video");
Bitmap bMap = null;
File spec;
if (mediaType == 3) {
spec = new File(vidFolder, file);
bMap = ThumbnailUtils.createVideoThumbnail(spec.getAbsolutePath(), MediaStore.Video.Thumbnails.MICRO_KIND);
} else if(mediaType == 2) {
spec = new File(audFolder, file);
}else{
spec = new File(imgFolder, file);
bMap = BitmapFactory.decodeFile(spec.getAbsolutePath());
}
long mediaSize = (file.equals("")) ? 0 : spec.length();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
if(mediaType == 1 || mediaType ==3) {
bMap = Bitmap.createScaledBitmap(bMap, 100, 59, false);
bMap.compress(Bitmap.CompressFormat.JPEG, 60, bos);
}
byte[] bArray = bos.toByteArray();
MediaData md = new MediaData();
md.fileSize = mediaSize;
md.file = spec;
md.autodownloadRetryEnabled = true;
byte[] arr = SerializationUtils.serialize(md);
initialValues.put("thumb_image", arr);
initialValues.put("quoted_row_id", 0);
//initialValues.put("media_mime_type", mimeType);
//initialValues.put("media_hash", "9vZ3oZyplgiZ40jJvo/sLNrk3c1fuLOA+hLEhEjL+rg=");
initialValues.put("raw_data", bArray);
initialValues.put("media_size", mediaSize);
initialValues.put("origin", 0);
initialValues.put("media_caption", msg);
} else
initialValues.put("data", msg);
long idm = db.insert("messages", null, initialValues);
query1 = " insert into chat_list (key_remote_jid) select '" + jid
+ "' where not exists (select 1 from chat_list where key_remote_jid='" + jid + "');";
query2 = " update chat_list set message_table_id = (select max(messages._id) from messages) where chat_list.key_remote_jid='" + jid + "';";
ContentValues values = new ContentValues();
values.put("docid", idm);
values.put("c0content", "null ");
db.insert("messages_fts_content", null, values);
db.execSQL(query1 + query2);
db.close();
}
After reading the notification, check if it has a reply action then use RemoteInput to reply on the notification.
Check this answer
https://stackoverflow.com/a/73017178/13222541

Loading Artists from Android MediaStore along with Artwork

I am aware that media artwork is stored under albums and to get them you need to have the album id to access it. I have been able to get the images for tracks and albums using the album id.
However for artists table doesn't have the album id field. Other apps such as Play Music and Poweramp are somehow able to get the track artwork and add them to the respective artists.
How do i achieve this?
The way I do it is to get all albums for an artist and then use the rnd function to return an albumid:
String artist_id = c.getString(c.getColumnIndex(MediaStore.Audio.Artists._ID));
Cursor crs = album.getArtistsAlbumcursor(mContext, artist_id);
if(crs!=null && crs.moveToFirst()) {
Random rn = new Random();
int rnd = rn.nextInt( crs.getCount());
crs.moveToPosition(rnd);
album_id = crs.getLong(crs.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
crs.close();
}
where getArtistsAlbumcursor is:
public Cursor getArtistsAlbumcursor(Context context, String artistId) {
ContentResolver cr = context.getContentResolver();
final String _id = MediaStore.Audio.Media._ID;
final String album_id = MediaStore.Audio.Media.ALBUM_ID;
final String artistid = MediaStore.Audio.Media.ARTIST_ID;
final String[] columns = { _id, album_id, artistid };
String where = artistid +" =?";
String[] aId = {artistId};
return cr.query(uri, columns, where, aId, null);
}
Once you have an albumid you can get your albumart using your original method.
Or
if you want to get the albumart from the mp3 track itself, you will need to implement a libary such as jaudiotagger or org.blinkenlights.jid3.v2.
Life gets a little more complicated but below how to get albumart from the mp3 tag using the JID3 library:
try {
bmp = getmp3AlbumArt(sourceFile);
} catch (Exception e) {
e.printStackTrace();
}
where getmp3Albumart is:
public Bitmap getmp3AlbumArt(File SourceFile) throws Exception {
Bitmap bmp = null;
arrayByte = null;
APICID3V2Frame frames[];
MediaFile MediaFile = new MP3File(SourceFile);
try {
Object obj = null;
obj = MediaFile.getID3V2Tag();
if (obj != null) {
tagImage = (org.blinkenlights.jid3.v2.ID3V2_3_0Tag) obj;
if ((tagImage != null) && (arrayByte == null) && (tagImage.getAPICFrames() != null) && (tagImage.getAPICFrames().length > 0)) {
frames = tagImage.getAPICFrames();
for (int i = 0; i < tagImage.getAPICFrames().length; i++) {
if (frames[i] != null) {
arrayByte = frames[i].getPictureData();
break;
}
}
}
}
} catch (ID3Exception | OutOfMemoryError e) {
e.printStackTrace();
}
if (arrayByte != null) {
try {
bmp = BitmapFactory.decodeByteArray(arrayByte, 0, arrayByte.length);
} catch (Exception|OutOfMemoryError e) {
e.printStackTrace();
}
}
return bmp;
}

getting AndroidRuntime: FATAL EXCEPTION: AsyncTask #1

I want to use AsyncTask to parse JSON data For that I have Created constructor of FetchWeatherTask in ForecastFragment
ForecastFragment.java
public class ForecastFragment extends Fragment {
private ArrayAdapter<String> mForecastAdapter;
public ForecastFragment() {
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Add this line in order for this fragment to handle menu events.
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.forecastfragment, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_refresh) {
updateWeather();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// The ArrayAdapter will take data from a source and
// use it to populate the ListView it's attached to.
mForecastAdapter =
new ArrayAdapter<String>(
getActivity(),// The current context (this activity)
R.layout.list_item_forecast,// The name of the layout ID.
R.id.tv_list_item_forecast, new ArrayList<String>()); // The ID of the textview to populate.
// Log.e("weekForecast", "forecastArray: " + forecastArray + "/n" + weekForecast);
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
listView.setAdapter(mForecastAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
String forecast = mForecastAdapter.getItem(position);
Intent intent = new Intent(getActivity(), DetailActivity.class)
.putExtra(Intent.EXTRA_TEXT, forecast);
startActivity(intent);
}
});
return rootView;
}
private void updateWeather() {
// FetchWeatherTask weatherTask = new FetchWeatherTask();
FetchWeatherTask weatherTask = new FetchWeatherTask(getActivity(), mForecastAdapter);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
String location = prefs.getString(getString(R.string.pref_location_key),
getString(R.string.pref_location_default));
weatherTask.execute(location);
}
#Override
public void onStart() {
super.onStart();
updateWeather();
}
FetchWeatherTask.java
public class FetchWeatherTask extends AsyncTask<String, Void, String[]> {
private final String LOG_TAG = FetchWeatherTask.class.getSimpleName();
private ArrayAdapter<String> mForecastAdapter;
private final Context mContext;
public FetchWeatherTask(Context context, ArrayAdapter<String> forecastAdapter) {
mContext = context;
mForecastAdapter = forecastAdapter;
}
private boolean DEBUG = true;
/* The date/time conversion code is going to be moved outside the asynctask later,
* so for convenience we're breaking it out into its own method now.
*/
private String getReadableDateString(long time) {
// Because the API returns a unix timestamp (measured in seconds),
// it must be converted to milliseconds in order to be converted to valid date.
Date date = new Date(time);
SimpleDateFormat format = new SimpleDateFormat("E, MMM d");
return format.format(date).toString();
}
/**
* Prepare the weather high/lows for presentation.
*/
private String formatHighLows(double high, double low) {
// Data is fetched in Celsius by default.
// If user prefers to see in Fahrenheit, convert the values here.
// We do this rather than fetching in Fahrenheit so that the user can
// change this option without us having to re-fetch the data once
// we start storing the values in a database.
SharedPreferences sharedPrefs =
PreferenceManager.getDefaultSharedPreferences(mContext);
String unitType = sharedPrefs.getString(
mContext.getString(R.string.pref_units_key),
mContext.getString(R.string.pref_units_metric));
if (unitType.equals(mContext.getString(R.string.pref_units_imperial))) {
high = (high * 1.8) + 32;
low = (low * 1.8) + 32;
} else if (!unitType.equals(mContext.getString(R.string.pref_units_metric))) {
Log.d(LOG_TAG, "Unit type not found: " + unitType);
}
// For presentation, assume the user doesn't care about tenths of a degree.
long roundedHigh = Math.round(high);
long roundedLow = Math.round(low);
String highLowStr = roundedHigh + "/" + roundedLow;
return highLowStr;
}
/**
* Helper method to handle insertion of a new location in the weather database.
*
* #param locationSetting The location string used to request updates from the server.
* #param cityName A human-readable city name, e.g "Mountain View"
* #param lat the latitude of the city
* #param lon the longitude of the city
* #return the row ID of the added location.
*/
long addLocation(String locationSetting, String cityName, double lat, double lon) {
long locationId;
// First, check if the location with this city name exists in the db
Cursor locationCursor = mContext.getContentResolver().query(
WeatherContract.LocationEntry.CONTENT_URI,
new String[]{WeatherContract.LocationEntry._ID},
WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " = ?",
new String[]{locationSetting},
null);
if (locationCursor.moveToFirst()) {
int locationIdIndex = locationCursor.getColumnIndex(WeatherContract.LocationEntry._ID);
locationId = locationCursor.getLong(locationIdIndex);
} else {
// Now that the content provider is set up, inserting rows of data is pretty simple.
// First create a ContentValues object to hold the data you want to insert.
ContentValues locationValues = new ContentValues();
// Then add the data, along with the corresponding name of the data type,
// so the content provider knows what kind of value is being inserted.
locationValues.put(WeatherContract.LocationEntry.COLUMN_CITY_NAME, cityName);
locationValues.put(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, locationSetting);
locationValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LAT, lat);
locationValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LONG, lon);
// Finally, insert location data into the database.
Uri insertedUri = mContext.getContentResolver().insert(
WeatherContract.LocationEntry.CONTENT_URI,
locationValues
);
// The resulting URI contains the ID for the row. Extract the locationId from the Uri.
locationId = ContentUris.parseId(insertedUri);
}
locationCursor.close();
// Wait, that worked? Yes!
return locationId;
}
/*
Students: This code will allow the FetchWeatherTask to continue to return the strings that
the UX expects so that we can continue to test the application even once we begin using
the database.
*/
String[] convertContentValuesToUXFormat(Vector<ContentValues> cvv) {
// return strings to keep UI functional for now
String[] resultStrs = new String[cvv.size()];
for (int i = 0; i < cvv.size(); i++) {
ContentValues weatherValues = cvv.elementAt(i);
String highAndLow = formatHighLows(
weatherValues.getAsDouble(WeatherEntry.COLUMN_MAX_TEMP),
weatherValues.getAsDouble(WeatherEntry.COLUMN_MIN_TEMP));
resultStrs[i] = getReadableDateString(
weatherValues.getAsLong(WeatherEntry.COLUMN_DATE)) +
" - " + weatherValues.getAsString(WeatherEntry.COLUMN_SHORT_DESC) +
" - " + highAndLow;
}
return resultStrs;
}
/**
* Take the String representing the complete forecast in JSON Format and
* pull out the data we need to construct the Strings needed for the wireframes.
* <p/>
* Fortunately parsing is easy: constructor takes the JSON string and converts it
* into an Object hierarchy for us.
*/
private String[] getWeatherDataFromJson(String forecastJsonStr,
String locationSetting)
throws JSONException {
// Now we have a String representing the complete forecast in JSON Format.
// Fortunately parsing is easy: constructor takes the JSON string and converts it
// into an Object hierarchy for us.
// These are the names of the JSON objects that need to be extracted.
// Location information
final String OWM_CITY = "city";
final String OWM_CITY_NAME = "name";
final String OWM_COORD = "coord";
// Location coordinate
final String OWM_LATITUDE = "lat";
final String OWM_LONGITUDE = "lon";
// Weather information. Each day's forecast info is an element of the "list" array.
final String OWM_LIST = "list";
final String OWM_PRESSURE = "pressure";
final String OWM_HUMIDITY = "humidity";
final String OWM_WINDSPEED = "speed";
final String OWM_WIND_DIRECTION = "deg";
// All temperatures are children of the "temp" object.
final String OWM_TEMPERATURE = "temp";
final String OWM_MAX = "max";
final String OWM_MIN = "min";
final String OWM_WEATHER = "weather";
final String OWM_DESCRIPTION = "main";
final String OWM_WEATHER_ID = "id";
try {
JSONObject forecastJson = new JSONObject(forecastJsonStr);
JSONArray weatherArray = forecastJson.getJSONArray(OWM_LIST);
JSONObject cityJson = forecastJson.getJSONObject(OWM_CITY);
String cityName = cityJson.getString(OWM_CITY_NAME);
JSONObject cityCoord = cityJson.getJSONObject(OWM_COORD);
double cityLatitude = cityCoord.getDouble(OWM_LATITUDE);
double cityLongitude = cityCoord.getDouble(OWM_LONGITUDE);
long locationId = addLocation(locationSetting, cityName, cityLatitude, cityLongitude);
// Insert the new weather information into the database
Vector<ContentValues> cVVector = new Vector<ContentValues>(weatherArray.length());
// OWM returns daily forecasts based upon the local time of the city that is being
// asked for, which means that we need to know the GMT offset to translate this data
// properly.
// Since this data is also sent in-order and the first day is always the
// current day, we're going to take advantage of that to get a nice
// normalized UTC date for all of our weather.
Time dayTime = new Time();
dayTime.setToNow();
// we start at the day returned by local time. Otherwise this is a mess.
int julianStartDay = Time.getJulianDay(System.currentTimeMillis(), dayTime.gmtoff);
// now we work exclusively in UTC
dayTime = new Time();
for (int i = 0; i < weatherArray.length(); i++) {
// These are the values that will be collected.
long dateTime;
double pressure;
int humidity;
double windSpeed;
double windDirection;
double high;
double low;
String description;
int weatherId;
// Get the JSON object representing the day
JSONObject dayForecast = weatherArray.getJSONObject(i);
// Cheating to convert this to UTC time, which is what we want anyhow
dateTime = dayTime.setJulianDay(julianStartDay + i);
pressure = dayForecast.getDouble(OWM_PRESSURE);
humidity = dayForecast.getInt(OWM_HUMIDITY);
windSpeed = dayForecast.getDouble(OWM_WINDSPEED);
windDirection = dayForecast.getDouble(OWM_WIND_DIRECTION);
// Description is in a child array called "weather", which is 1 element long.
// That element also contains a weather code.
JSONObject weatherObject =
dayForecast.getJSONArray(OWM_WEATHER).getJSONObject(0);
description = weatherObject.getString(OWM_DESCRIPTION);
weatherId = weatherObject.getInt(OWM_WEATHER_ID);
// Temperatures are in a child object called "temp". Try not to name variables
// "temp" when working with temperature. It confuses everybody.
JSONObject temperatureObject = dayForecast.getJSONObject(OWM_TEMPERATURE);
high = temperatureObject.getDouble(OWM_MAX);
low = temperatureObject.getDouble(OWM_MIN);
ContentValues weatherValues = new ContentValues();
weatherValues.put(WeatherEntry.COLUMN_LOC_KEY, locationId);
weatherValues.put(WeatherEntry.COLUMN_DATE, dateTime);
weatherValues.put(WeatherEntry.COLUMN_HUMIDITY, humidity);
weatherValues.put(WeatherEntry.COLUMN_PRESSURE, pressure);
weatherValues.put(WeatherEntry.COLUMN_WIND_SPEED, windSpeed);
weatherValues.put(WeatherEntry.COLUMN_DEGREES, windDirection);
weatherValues.put(WeatherEntry.COLUMN_MAX_TEMP, high);
weatherValues.put(WeatherEntry.COLUMN_MIN_TEMP, low);
weatherValues.put(WeatherEntry.COLUMN_SHORT_DESC, description);
weatherValues.put(WeatherEntry.COLUMN_WEATHER_ID, weatherId);
cVVector.add(weatherValues);
}
// add to database
if (cVVector.size() > 0) {
ContentValues[] cvArray = new ContentValues[cVVector.size()];
cVVector.toArray(cvArray);
mContext.getContentResolver().bulkInsert(WeatherEntry.CONTENT_URI, cvArray);
}
// Sort order: Ascending, by date.
String sortOrder = WeatherEntry.COLUMN_DATE + " ASC";
Uri weatherForLocationUri = WeatherEntry.buildWeatherLocationWithStartDate(
locationSetting, System.currentTimeMillis());
// Students: Uncomment the next lines to display what what you stored in the bulkInsert
Cursor cur = mContext.getContentResolver().query(weatherForLocationUri,
null, null, null, sortOrder);
cVVector = new Vector<ContentValues>(cur.getCount());
if (cur.moveToFirst()) {
do {
ContentValues cv = new ContentValues();
DatabaseUtils.cursorRowToContentValues(cur, cv);
cVVector.add(cv);
} while (cur.moveToNext());
}
Log.d(LOG_TAG, "FetchWeatherTask Complete. " + cVVector.size() + " Inserted");
String[] resultStrs = convertContentValuesToUXFormat(cVVector);
return resultStrs;
} catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
e.printStackTrace();
}
return null;
}
#Override
protected String[] doInBackground(String... params) {
// If there's no zip code, there's nothing to look up. Verify size of params.
if (params.length == 0) {
return null;
}
String locationQuery = params[0];
// These two need to be declared outside the try/catch
// so that they can be closed in the finally block.
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
// Will contain the raw JSON response as a string.
String forecastJsonStr = null;
String format = "json";
String units = "metric";
int numDays = 14;
try {
// Construct the URL for the OpenWeatherMap query
// Possible parameters are avaiable at OWM's forecast API page, at
// http://openweathermap.org/API#forecast
final String FORECAST_BASE_URL =
"http://api.openweathermap.org/data/2.5/forecast/daily?";
final String QUERY_PARAM = "q";
final String FORMAT_PARAM = "mode";
final String UNITS_PARAM = "units";
final String DAYS_PARAM = "cnt";
final String APPID_PARAM = "APPID";
Uri builtUri = Uri.parse(FORECAST_BASE_URL).buildUpon()
.appendQueryParameter(QUERY_PARAM, params[0])
.appendQueryParameter(FORMAT_PARAM, format)
.appendQueryParameter(UNITS_PARAM, units)
.appendQueryParameter(DAYS_PARAM, Integer.toString(numDays))
.appendQueryParameter(APPID_PARAM, BuildConfig.OPEN_WEATHER_MAP_API_KEY)
.build();
URL url = new URL(builtUri.toString());
// Create the request to OpenWeatherMap, and open the connection
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// Read the input stream into a String
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// Nothing to do.
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
// Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
// But it does make debugging a *lot* easier if you print out the completed
// buffer for debugging.
buffer.append(line + "\n");
}
if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
return null;
}
forecastJsonStr = buffer.toString();
} catch (IOException e) {
Log.e(LOG_TAG, "Error ", e);
// If the code didn't successfully get the weather data, there's no point in attemping
// to parse it.
return null;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e(LOG_TAG, "Error closing stream", e);
}
}
}
try {
return getWeatherDataFromJson(forecastJsonStr, locationQuery);
} catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
e.printStackTrace();
}
// This will only happen if there was an error getting or parsing the forecast.
return null;
}
#Override
protected void onPostExecute(String[] result) {
if (result != null && mForecastAdapter != null) {
mForecastAdapter.clear();
for (String dayForecastStr : result) {
mForecastAdapter.add(dayForecastStr);
}
// New data is back from the server. Hooray!
}
}}
Though I have created constructor of FetchWeatherTask and initialized the values but still I am getting Following error:
http://i.stack.imgur.com/6elr4.png
Your
locationCursor
is null in
addLocation
method

Android: android:autoLink=“web” does not support query parameters after domains

I have a TextView with autoLink set as
<TextView
android:id="#+id/messageDetail_privateText_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web|phone|email" />
But when I set a text with an url like http://www.test.com?p1=v1&p2=v2 the TextView's autolink doesn't recognize the query parameters after the domain.
I can understand that this kind of URL's doesn't have too much sense, but is there any workaround to this problem?
iOS is recognizing the parameters just fine.
Answering to my own question, what finally worked for me was to check the urls of the string and adding the slash manually. Not the coolest solution in the world but worked in this case.
Below the code:
protected String normalizeURLs(String html)
{
String[] pieces = html.split(" ");
ArrayList<String> textParts = new ArrayList<>();
for(String piece : pieces) {
try {
URL isURL = new URL(piece);
String protocol = isURL.getProtocol();
String host = isURL.getHost();
String query = isURL.getQuery();
String path = isURL.getPath();
String questionMark = "?";
if (path.equals("")) {
path = "/";
}
if (query == null) {
query = "";
questionMark = "";
}
String url = protocol + "://" + host + path + questionMark + query;
textParts.add(url);
} catch (MalformedURLException exception) {
textParts.add(piece);
}
}
String resultString = "";
for (String s : textParts)
{
resultString += s + " ";
}
return resultString;
}

Categories

Resources