Android Open Source - TurkcellUpdater_android_sdk Update Entry






From Project

Back to project page TurkcellUpdater_android_sdk.

License

The source code is released under:

Apache License

If you think the Android project TurkcellUpdater_android_sdk listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*******************************************************************************
 */*w  w  w.  ja  va2s .  c o m*/
 *  Copyright (C) 2013 Turkcell
 *  
 *  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 tr.com.turkcellteknoloji.turkcellupdater;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.json.JSONObject;


class UpdateEntry extends FilteredEntry {
  final List<UpdateDescription> updateDescriptions;

  final int targetVersionCode;
  final String targetPackageName;

  final URL targetPackageUrl;
  final URL targetWebsiteUrl;
  final boolean targetGooglePlay;

  final boolean forceUpdate;
  final boolean forceExit;

  UpdateEntry(List<Filter> filters,
      List<UpdateDescription> updateDescriptions, int targetVersionCode,
      String targetPackageName, URL targetPackageUrl,
      URL targetWebsiteUrl, boolean targetGooglePlay, boolean forceUpdate, boolean forceExit)
      throws UpdaterException {
    super(filters);
    this.updateDescriptions = updateDescriptions;
    this.targetVersionCode = targetVersionCode;
    this.targetPackageName = targetPackageName;
    this.targetPackageUrl = targetPackageUrl;
    this.targetWebsiteUrl = targetWebsiteUrl;
    this.targetGooglePlay = targetGooglePlay;
    this.forceUpdate = forceUpdate;
    this.forceExit = forceExit;

    validate();
  }

  UpdateEntry(JSONObject jsonObject) throws UpdaterException {
    super(jsonObject);

    this.targetVersionCode = jsonObject.optInt("targetVersionCode", -1);
    this.forceUpdate = jsonObject.optBoolean("forceUpdate");
    this.forceExit = jsonObject.optBoolean("forceExit");
    this.updateDescriptions = createUpdateDescritions(jsonObject);

    this.targetPackageUrl = getUrl(jsonObject, "targetPackageUrl");

    this.targetWebsiteUrl = getUrl(jsonObject, "targetWebsiteUrl");

    this.targetPackageName = Utilities.removeWhiteSpaces(jsonObject
        .optString("targetPackageName"));

    this.targetGooglePlay = jsonObject.optBoolean("targetGooglePlay");

    validate();
  }

  private static URL getUrl(JSONObject jsonObject, String key)
      throws UpdaterException {
    String spec = Utilities.removeWhiteSpaces(jsonObject.optString(key));
    if ("".equals(spec)) {
      return null;
    }

    try {
      return new URL(spec);
    } catch (MalformedURLException e) {
      throw new UpdaterException("'" + key + "' url is malformatted", e);
    }
  }

  private static List<UpdateDescription> createUpdateDescritions(
      JSONObject jsonObject) {
    final List<UpdateDescription> result = new Vector<UpdateDescription>();
    final JSONObject udsObject = jsonObject
        .optJSONObject("descriptions");

    if (udsObject != null) {
      Iterator<?> languages = udsObject.keys();
      while (languages.hasNext()) {
        String languageCode = languages.next().toString();
        final JSONObject o = udsObject.optJSONObject(languageCode);
        if (o != null) {
          UpdateDescription ud = new UpdateDescription(languageCode,
              o);
          result.add(ud);
        } else {
          final String s = udsObject.optString(languageCode, null);
          if (s != null) {
            UpdateDescription ud = new UpdateDescription(
                languageCode, s);
            result.add(ud);
          }
        }

      }
    }

    return result;
  }

  Update getUpdate(Properties properties) throws UpdaterException {
    String languageCode = null;
    if(properties!=null) {
      final String s = properties.getValue(Properties.KEY_DEVICE_LANGUAGE);
      if(!Utilities.isNullOrEmpty(s)) {
        languageCode = s;
      }

    }

    String packageName = this.targetPackageName;
    if(Utilities.isNullOrEmpty(packageName) && properties!=null) {
      packageName = properties.getValue(Properties.KEY_APP_PACKAGE_NAME);
    }
    if(Utilities.isNullOrEmpty(packageName)) {
      throw new UpdaterException("'packageName' property should not be null or empty.");
    }

    final UpdateDescription updateDescription = LocalizedStringMap.select(updateDescriptions, languageCode);
    return new Update(updateDescription,
        targetPackageUrl, targetWebsiteUrl, targetGooglePlay, targetVersionCode,
        packageName, forceUpdate, forceExit);
  }

  private void validate() throws UpdaterException {
    if(forceExit) {
      return;
    }


    if (targetVersionCode < 0) {
      throw new UpdaterException(
          "'targetVersionCode' shoud be a positive number. Current value: "
              + targetVersionCode);
    }

    if (targetPackageUrl == null && targetWebsiteUrl == null && !targetGooglePlay) {
      throw new UpdaterException(
          "At least one of 'targetWebsiteUrl' and 'targetPackageUrl' shoud not be a null or 'targetGooglePlay' should be true.");
    }
  }

  boolean shouldDisplay(Properties properties) {
    if(isMatches(properties)) {

      final Integer currentVersionCode = Utilities.tryParseInteger(properties.getValue(Properties.KEY_APP_VERSION_CODE));
      if(currentVersionCode!=null && targetVersionCode != currentVersionCode.intValue()) {
        return true;
      }
      final String currentPackageName = properties.getValue(Properties.KEY_APP_PACKAGE_NAME);
      if(!Utilities.isNullOrEmpty(currentPackageName) && !Utilities.isNullOrEmpty(targetPackageName)) {
        return !currentPackageName.equals(targetPackageName);
      }
    }

    return false;
  }
}




Java Source Code List

tr.com.turkcellteknoloji.turkcellupdater.Configuration.java
tr.com.turkcellteknoloji.turkcellupdater.DownloadHandler.java
tr.com.turkcellteknoloji.turkcellupdater.DownloadRequest.java
tr.com.turkcellteknoloji.turkcellupdater.Filter.java
tr.com.turkcellteknoloji.turkcellupdater.FilteredEntry.java
tr.com.turkcellteknoloji.turkcellupdater.LocalizedStringMap.java
tr.com.turkcellteknoloji.turkcellupdater.Log.java
tr.com.turkcellteknoloji.turkcellupdater.MessageDescription.java
tr.com.turkcellteknoloji.turkcellupdater.MessageDisplayRecords.java
tr.com.turkcellteknoloji.turkcellupdater.MessageEntry.java
tr.com.turkcellteknoloji.turkcellupdater.Message.java
tr.com.turkcellteknoloji.turkcellupdater.Properties.java
tr.com.turkcellteknoloji.turkcellupdater.RestFailureHandler.java
tr.com.turkcellteknoloji.turkcellupdater.RestJsonObjectResultHandler.java
tr.com.turkcellteknoloji.turkcellupdater.RestNoValueResultHandler.java
tr.com.turkcellteknoloji.turkcellupdater.RestRequest.java
tr.com.turkcellteknoloji.turkcellupdater.UpdateDescription.java
tr.com.turkcellteknoloji.turkcellupdater.UpdateEntry.java
tr.com.turkcellteknoloji.turkcellupdater.UpdateManager.java
tr.com.turkcellteknoloji.turkcellupdater.Update.java
tr.com.turkcellteknoloji.turkcellupdater.UpdaterDialogManager.java
tr.com.turkcellteknoloji.turkcellupdater.UpdaterException.java
tr.com.turkcellteknoloji.turkcellupdater.Utilities.java
tr.com.turkcellteknoloji.turkcellupdater.VersionsMap.java
tr.com.turkcellteknoloji.turkcellupdater.package-info.java
tr.com.turkcellteknoloji.turkcellupdatersampleapp.LoginActivity.java
tr.com.turkcellteknoloji.turkcellupdatersampleapp.SplashActivity.java