extends ArrayAdapter to create URL history : Url « Network « Android






extends ArrayAdapter to create URL history

 
/***
Copyright (c) 2008-2009 CommonsWare, LLC
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 com.mediaportal.ampdroid.videoplayer;

import android.content.Context;
import android.widget.ArrayAdapter;
import java.util.ArrayList;
import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;

public class URLHistory extends ArrayAdapter<URLHistory.HistoryItem> {
   private ArrayList<URLHistory.HistoryItem> spareCopy = new ArrayList<URLHistory.HistoryItem>();

   public URLHistory(Context context, int resource) {
      super(context, resource, new ArrayList<URLHistory.HistoryItem>());
   }

   public void update(String url) {
      for (HistoryItem h : spareCopy) {
         if (url.equals(h.url)) {
            h.count++;
            return;
         }
      }

      HistoryItem h = new HistoryItem(url);

      spareCopy.add(h);
      add(h); // duplicate due to moronic filtering
   }

   public void save(Writer out) throws JSONException, IOException {
      JSONStringer json = new JSONStringer().object();

      for (HistoryItem h : spareCopy) {
         h.emit(json);
      }

      out.write(json.endObject().toString());
   }

   public void load(String rawJSON) throws JSONException {
      JSONObject json = new JSONObject(rawJSON);

      for (Iterator i = json.keys(); i.hasNext();) {
         String key = i.next().toString();
         HistoryItem h = new HistoryItem(key, json.getInt(key));

         spareCopy.add(h);
         add(h);
      }
   }

   class HistoryItem {
      String url = null;
      int count = 1;

      HistoryItem(String url) {
         this.url = url;
      }

      HistoryItem(String url, int count) {
         this.url = url;
         this.count = count;
      }

      public String toString() {
         return (url);
      }

      void emit(JSONStringer json) throws JSONException {
         json.key(url).value(count);
      }
   }
}

   
  








Related examples in the same category

1.Using Intent to open a URL
2.Process xml document from a Url
3.Suggest Url Provider
4.Showing android:autoLink property, which linkifies things like URLs and phone numbers found in the text.
5.Used to compress URL using the bit.ly service.
6.URL encode and decode
7.Is valid URL
8.Download from URLConnection
9.Request from an URL
10.Get Url From Img Tag
11.Returns the contents of the given URL as an array of strings
12.Read from a URL
13.Pull the raw text content of the given URL.
14.Get Video from URL
15.Gets data from URL
16.get Url Response
17.URL Encode Utils
18.Downloads a file given URL to specified destination
19.get Host From Url
20.encode Url
21.decode Url
22.Convert a byte array to a URL encoded string
23.get Url content with max retries
24.get Url By Post
25.Take a base url and a {@link Map} of parameters to build a valid url