Search in sources :

Example 1 with Geocoder

use of android.location.Geocoder in project Talon-for-Twitter by klinker24.

the class TweetFragment method getInfo.

public void getInfo(final View favButton, final TextView favCount, final TextView retweetCount, final long tweetId, final View retweetButton) {
    Thread getInfo = new Thread(new Runnable() {

        @Override
        public void run() {
            String location = "";
            String via = "";
            long realTime = 0;
            boolean retweetedByMe = false;
            try {
                Twitter twitter = getTwitter();
                TwitterMultipleImageHelper helper = new TwitterMultipleImageHelper();
                status = twitter.showStatus(tweetId);
                ArrayList<String> i = new ArrayList<String>();
                if (picture) {
                    i = helper.getImageURLs(status, twitter);
                }
                final ArrayList<String> images = i;
                GeoLocation loc = status.getGeoLocation();
                try {
                    Geocoder geocoder = new Geocoder(context, Locale.getDefault());
                    List<Address> addresses = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
                    if (addresses.size() > 0) {
                        Address address = addresses.get(0);
                        location += address.getLocality() + ", " + address.getCountryName();
                    } else {
                        location = "";
                    }
                } catch (Exception x) {
                    location = "";
                }
                via = android.text.Html.fromHtml(status.getSource()).toString();
                final String sfavCount;
                if (status.isRetweet()) {
                    twitter4j.Status status2 = status.getRetweetedStatus();
                    via = android.text.Html.fromHtml(status2.getSource()).toString();
                    realTime = status2.getCreatedAt().getTime();
                    sfavCount = status2.getFavoriteCount() + "";
                } else {
                    realTime = status.getCreatedAt().getTime();
                    sfavCount = status.getFavoriteCount() + "";
                }
                retweetedByMe = status.isRetweetedByMe();
                final String retCount = "" + status.getRetweetCount();
                final String timeDisplay;
                if (!settings.militaryTime) {
                    timeDisplay = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.US).format(realTime) + " " + DateFormat.getTimeInstance(DateFormat.SHORT, Locale.US).format(realTime);
                } else {
                    timeDisplay = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.GERMAN).format(realTime) + " " + DateFormat.getTimeInstance(DateFormat.SHORT, Locale.GERMAN).format(realTime);
                }
                final String fVia = " " + getResources().getString(R.string.via) + " " + via;
                final String fLoc = location.equals("") ? "" : "\n" + location;
                final boolean fRet = retweetedByMe;
                final long fTime = realTime;
                final Status fStatus = status;
                ((Activity) context).runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        retweetCount.setText(" " + retCount);
                        if (retweetButton instanceof ImageButton) {
                            if (fRet) {
                                if (!settings.addonTheme) {
                                    ((ImageButton) retweetButton).setColorFilter(context.getResources().getColor(R.color.app_color));
                                } else {
                                    ((ImageButton) retweetButton).setColorFilter(settings.accentInt);
                                }
                            } else {
                                ((ImageButton) retweetButton).clearColorFilter();
                            }
                        } else {
                            if (fRet) {
                                if (!settings.addonTheme) {
                                    retweetButton.setBackgroundColor(context.getResources().getColor(R.color.app_color));
                                } else {
                                    retweetButton.setBackgroundColor(settings.accentInt);
                                }
                            } else {
                                retweetButton.setBackgroundColor(getResources().getColor(android.R.color.transparent));
                            }
                        }
                        timetv.setText(timeDisplay + fVia);
                        timetv.append(fLoc);
                        favCount.setText(" " + sfavCount);
                        if (favButton instanceof ImageButton) {
                            if (fStatus.isFavorited()) {
                                TypedArray a = context.getTheme().obtainStyledAttributes(new int[] { R.attr.favoritedButton });
                                int resource = a.getResourceId(0, 0);
                                a.recycle();
                                if (!settings.addonTheme) {
                                    ((ImageButton) favButton).setColorFilter(context.getResources().getColor(R.color.app_color));
                                } else {
                                    ((ImageButton) favButton).setColorFilter(settings.accentInt);
                                }
                                ((ImageButton) favButton).setImageDrawable(context.getResources().getDrawable(resource));
                                isFavorited = true;
                            } else {
                                TypedArray a = context.getTheme().obtainStyledAttributes(new int[] { R.attr.notFavoritedButton });
                                int resource = a.getResourceId(0, 0);
                                a.recycle();
                                ((ImageButton) favButton).setImageDrawable(context.getResources().getDrawable(resource));
                                isFavorited = false;
                                ((ImageButton) favButton).clearColorFilter();
                            }
                        } else {
                            if (fStatus.isFavorited()) {
                                TypedArray a = context.getTheme().obtainStyledAttributes(new int[] { R.attr.favoritedButton });
                                int resource = a.getResourceId(0, 0);
                                a.recycle();
                                if (!settings.addonTheme) {
                                    favButton.setBackgroundColor(context.getResources().getColor(R.color.app_color));
                                } else {
                                    favButton.setBackgroundColor(settings.accentInt);
                                }
                                isFavorited = true;
                            } else {
                                isFavorited = false;
                                favButton.setBackgroundColor(getResources().getColor(android.R.color.transparent));
                            }
                        }
                        for (String s : images) {
                            Log.v("talon_image", s);
                        }
                        if (images.size() > 1) {
                            Log.v("talon_images", "size: " + images.size());
                            try {
                                mAttacher.setOnViewTapListener(new PhotoViewAttacher.OnViewTapListener() {

                                    @Override
                                    public void onViewTap(View view, float x, float y) {
                                        Intent viewPics = new Intent(context, ViewPictures.class);
                                        viewPics.putExtra("images", images);
                                        startActivity(viewPics);
                                    }
                                });
                            } catch (Exception e) {
                                // addon theme without the attacher
                                profilePic.setOnClickListener(new View.OnClickListener() {

                                    @Override
                                    public void onClick(View v) {
                                        Intent viewPics = new Intent(context, ViewPictures.class);
                                        viewPics.putExtra("images", images);
                                        startActivity(viewPics);
                                    }
                                });
                            }
                        }
                    }
                });
            } catch (Exception e) {
            }
        }
    });
    getInfo.setPriority(Thread.MAX_PRIORITY);
    getInfo.start();
}
Also used : ViewPictures(com.klinker.android.twitter.activities.tweet_viewer.ViewPictures) Address(android.location.Address) ArrayList(java.util.ArrayList) TwitterMultipleImageHelper(com.klinker.android.twitter.utils.api_helper.TwitterMultipleImageHelper) Intent(android.content.Intent) Geocoder(android.location.Geocoder) PhotoViewAttacher(uk.co.senab.photoview.PhotoViewAttacher) ImageView(android.widget.ImageView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Point(android.graphics.Point) ImageButton(android.widget.ImageButton) TypedArray(android.content.res.TypedArray) List(java.util.List) ArrayList(java.util.ArrayList)

Example 2 with Geocoder

use of android.location.Geocoder in project weiciyuan by qii.

the class GoogleGeoCoderDao method get.

public String get() {
    Geocoder geocoder = new Geocoder(activity, Locale.getDefault());
    List<Address> addresses = null;
    try {
        if (!Utility.isGPSLocationCorrect(geoBean)) {
            return null;
        }
        addresses = geocoder.getFromLocation(geoBean.getLat(), geoBean.getLon(), 1);
    } catch (IOException e) {
    }
    if (addresses != null && addresses.size() > 0) {
        Address address = addresses.get(0);
        StringBuilder builder = new StringBuilder();
        int size = address.getMaxAddressLineIndex();
        for (int i = 0; i < size; i++) {
            builder.append(address.getAddressLine(i));
        }
        return builder.toString();
    }
    return null;
}
Also used : Address(android.location.Address) IOException(java.io.IOException) Geocoder(android.location.Geocoder)

Example 3 with Geocoder

use of android.location.Geocoder in project muzei by romannurik.

the class GalleryArtSource method onCreate.

@Override
public void onCreate() {
    super.onCreate();
    mGeocoder = new Geocoder(this);
    mContentObserver = new ContentObserver(new Handler()) {

        @Override
        public void onChange(boolean selfChange, Uri uri) {
            // Update the metadata
            updateMeta();
            // See if we've just added the very first image
            Artwork currentArtwork = getCurrentArtwork();
            if (currentArtwork == null) {
                publishNextArtwork(null);
                return;
            }
            // See if the current artwork was removed
            Uri currentArtworkUri = currentArtwork.getToken() != null ? Uri.parse(currentArtwork.getToken()) : null;
            if (uri.equals(currentArtworkUri)) {
                // We're showing a removed URI
                publishNextArtwork(null);
            }
        }
    };
    // Make any changes since the last time the GalleryArtSource was created
    mContentObserver.onChange(false, GalleryContract.ChosenPhotos.CONTENT_URI);
    getContentResolver().registerContentObserver(GalleryContract.ChosenPhotos.CONTENT_URI, true, mContentObserver);
}
Also used : Artwork(com.google.android.apps.muzei.api.Artwork) Handler(android.os.Handler) Geocoder(android.location.Geocoder) Uri(android.net.Uri) ContentObserver(android.database.ContentObserver)

Example 4 with Geocoder

use of android.location.Geocoder in project WordPress-Android by wordpress-mobile.

the class GeocoderUtils method getAddressFromLocationName.

public static Address getAddressFromLocationName(Context context, String locationName) {
    int maxResults = 1;
    Address address = null;
    List<Address> addresses = null;
    Geocoder gcd = getGeocoder(context);
    if (gcd == null) {
        return null;
    }
    try {
        addresses = gcd.getFromLocationName(locationName, maxResults);
    } catch (IOException e) {
        AppLog.e(AppLog.T.UTILS, "Failed to get coordinates from location", e);
    }
    // addresses may be null or empty if network isn't connected
    if (addresses != null && addresses.size() > 0) {
        address = addresses.get(0);
    }
    return address;
}
Also used : Address(android.location.Address) IOException(java.io.IOException) Geocoder(android.location.Geocoder)

Example 5 with Geocoder

use of android.location.Geocoder in project android_frameworks_base by DirtyUnicorns.

the class GeocoderTest method testGeocoder.

public void testGeocoder() throws Exception {
    Locale locale = new Locale("en", "us");
    Geocoder g = new Geocoder(mContext, locale);
    List<Address> addresses1 = g.getFromLocation(37.435067, -122.166767, 2);
    assertNotNull(addresses1);
    assertEquals(1, addresses1.size());
    Address addr = addresses1.get(0);
    assertEquals("94305", addr.getFeatureName());
    assertEquals("Palo Alto, CA 94305", addr.getAddressLine(0));
    assertEquals("USA", addr.getAddressLine(1));
    assertEquals("94305", addr.getPostalCode());
    assertFalse(Math.abs(addr.getLatitude() - 37.4240385) > 0.1);
    List<Address> addresses2 = g.getFromLocationName("San Francisco, CA", 1);
    assertNotNull(addresses2);
    assertEquals(1, addresses2.size());
    addr = addresses2.get(0);
    assertEquals("San Francisco", addr.getFeatureName());
    assertEquals("San Francisco, CA", addr.getAddressLine(0));
    assertEquals("United States", addr.getAddressLine(1));
    assertEquals("San Francisco", addr.getLocality());
    assertEquals("CA", addr.getAdminArea());
    assertEquals(null, addr.getPostalCode());
    assertFalse(Math.abs(addr.getLatitude() - 37.77916) > 0.1);
}
Also used : Locale(java.util.Locale) Address(android.location.Address) Geocoder(android.location.Geocoder)

Aggregations

Geocoder (android.location.Geocoder)27 Address (android.location.Address)22 IOException (java.io.IOException)16 Locale (java.util.Locale)6 ArrayList (java.util.ArrayList)4 Location (android.location.Location)3 Handler (android.os.Handler)2 View (android.view.View)2 GoogleApiClient (com.google.android.gms.common.api.GoogleApiClient)2 List (java.util.List)2 SuppressLint (android.annotation.SuppressLint)1 ProgressDialog (android.app.ProgressDialog)1 Intent (android.content.Intent)1 TypedArray (android.content.res.TypedArray)1 ContentObserver (android.database.ContentObserver)1 Point (android.graphics.Point)1 LocationManager (android.location.LocationManager)1 Uri (android.net.Uri)1 HandlerThread (android.os.HandlerThread)1 SearchView (android.support.v7.widget.SearchView)1