137237839e87988208296a57d70767ebbd9c6880bJohn Grossman/* 237237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Copyright (C) 2012 The Android Open Source Project 337237839e87988208296a57d70767ebbd9c6880bJohn Grossman * 437237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Licensed under the Apache License, Version 2.0 (the "License"); 537237839e87988208296a57d70767ebbd9c6880bJohn Grossman * you may not use this file except in compliance with the License. 637237839e87988208296a57d70767ebbd9c6880bJohn Grossman * You may obtain a copy of the License at 737237839e87988208296a57d70767ebbd9c6880bJohn Grossman * 837237839e87988208296a57d70767ebbd9c6880bJohn Grossman * http://www.apache.org/licenses/LICENSE-2.0 937237839e87988208296a57d70767ebbd9c6880bJohn Grossman * 1037237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Unless required by applicable law or agreed to in writing, software 1137237839e87988208296a57d70767ebbd9c6880bJohn Grossman * distributed under the License is distributed on an "AS IS" BASIS, 1237237839e87988208296a57d70767ebbd9c6880bJohn Grossman * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1337237839e87988208296a57d70767ebbd9c6880bJohn Grossman * See the License for the specific language governing permissions and 1437237839e87988208296a57d70767ebbd9c6880bJohn Grossman * limitations under the License. 1537237839e87988208296a57d70767ebbd9c6880bJohn Grossman */ 1637237839e87988208296a57d70767ebbd9c6880bJohn Grossmanpackage android.os; 1737237839e87988208296a57d70767ebbd9c6880bJohn Grossman 1837237839e87988208296a57d70767ebbd9c6880bJohn Grossmanimport java.net.InetSocketAddress; 1937237839e87988208296a57d70767ebbd9c6880bJohn Grossmanimport java.util.NoSuchElementException; 2037237839e87988208296a57d70767ebbd9c6880bJohn Grossman 2137237839e87988208296a57d70767ebbd9c6880bJohn Grossmanimport android.os.CommonTimeUtils; 2237237839e87988208296a57d70767ebbd9c6880bJohn Grossmanimport android.os.IBinder; 2337237839e87988208296a57d70767ebbd9c6880bJohn Grossmanimport android.os.RemoteException; 2437237839e87988208296a57d70767ebbd9c6880bJohn Grossmanimport android.os.ServiceManager; 2537237839e87988208296a57d70767ebbd9c6880bJohn Grossman 2637237839e87988208296a57d70767ebbd9c6880bJohn Grossman/** 2737237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Used for configuring and controlling the status of the android common time service. 2837237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @hide 2937237839e87988208296a57d70767ebbd9c6880bJohn Grossman */ 3037237839e87988208296a57d70767ebbd9c6880bJohn Grossmanpublic class CommonTimeConfig { 3137237839e87988208296a57d70767ebbd9c6880bJohn Grossman /** 3237237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Successful operation. 3337237839e87988208296a57d70767ebbd9c6880bJohn Grossman */ 3437237839e87988208296a57d70767ebbd9c6880bJohn Grossman public static final int SUCCESS = 0; 3537237839e87988208296a57d70767ebbd9c6880bJohn Grossman /** 3637237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Unspecified error. 3737237839e87988208296a57d70767ebbd9c6880bJohn Grossman */ 3837237839e87988208296a57d70767ebbd9c6880bJohn Grossman public static final int ERROR = -1; 3937237839e87988208296a57d70767ebbd9c6880bJohn Grossman /** 4037237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Operation failed due to bad parameter value. 4137237839e87988208296a57d70767ebbd9c6880bJohn Grossman */ 4237237839e87988208296a57d70767ebbd9c6880bJohn Grossman public static final int ERROR_BAD_VALUE = -4; 4337237839e87988208296a57d70767ebbd9c6880bJohn Grossman /** 4437237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Operation failed due to dead remote object. 4537237839e87988208296a57d70767ebbd9c6880bJohn Grossman */ 4637237839e87988208296a57d70767ebbd9c6880bJohn Grossman public static final int ERROR_DEAD_OBJECT = -7; 4737237839e87988208296a57d70767ebbd9c6880bJohn Grossman 4837237839e87988208296a57d70767ebbd9c6880bJohn Grossman /** 4937237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Sentinel value returned by {@link #getMasterElectionGroupId()} when an error occurs trying to 5037237839e87988208296a57d70767ebbd9c6880bJohn Grossman * fetch the master election group. 5137237839e87988208296a57d70767ebbd9c6880bJohn Grossman */ 5237237839e87988208296a57d70767ebbd9c6880bJohn Grossman public static final long INVALID_GROUP_ID = -1; 5337237839e87988208296a57d70767ebbd9c6880bJohn Grossman 5437237839e87988208296a57d70767ebbd9c6880bJohn Grossman /** 5537237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Name of the underlying native binder service 5637237839e87988208296a57d70767ebbd9c6880bJohn Grossman */ 5737237839e87988208296a57d70767ebbd9c6880bJohn Grossman public static final String SERVICE_NAME = "common_time.config"; 5837237839e87988208296a57d70767ebbd9c6880bJohn Grossman 5937237839e87988208296a57d70767ebbd9c6880bJohn Grossman /** 6037237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Class constructor. 6137237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @throws android.os.RemoteException 6237237839e87988208296a57d70767ebbd9c6880bJohn Grossman */ 6337237839e87988208296a57d70767ebbd9c6880bJohn Grossman public CommonTimeConfig() 6437237839e87988208296a57d70767ebbd9c6880bJohn Grossman throws RemoteException { 6537237839e87988208296a57d70767ebbd9c6880bJohn Grossman mRemote = ServiceManager.getService(SERVICE_NAME); 6637237839e87988208296a57d70767ebbd9c6880bJohn Grossman if (null == mRemote) 6737237839e87988208296a57d70767ebbd9c6880bJohn Grossman throw new RemoteException(); 6837237839e87988208296a57d70767ebbd9c6880bJohn Grossman 6937237839e87988208296a57d70767ebbd9c6880bJohn Grossman mInterfaceDesc = mRemote.getInterfaceDescriptor(); 7037237839e87988208296a57d70767ebbd9c6880bJohn Grossman mUtils = new CommonTimeUtils(mRemote, mInterfaceDesc); 7137237839e87988208296a57d70767ebbd9c6880bJohn Grossman mRemote.linkToDeath(mDeathHandler, 0); 7237237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 7337237839e87988208296a57d70767ebbd9c6880bJohn Grossman 7437237839e87988208296a57d70767ebbd9c6880bJohn Grossman /** 7537237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Handy class factory method. 7637237839e87988208296a57d70767ebbd9c6880bJohn Grossman */ 7737237839e87988208296a57d70767ebbd9c6880bJohn Grossman static public CommonTimeConfig create() { 7837237839e87988208296a57d70767ebbd9c6880bJohn Grossman CommonTimeConfig retVal; 7937237839e87988208296a57d70767ebbd9c6880bJohn Grossman 8037237839e87988208296a57d70767ebbd9c6880bJohn Grossman try { 8137237839e87988208296a57d70767ebbd9c6880bJohn Grossman retVal = new CommonTimeConfig(); 8237237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 8337237839e87988208296a57d70767ebbd9c6880bJohn Grossman catch (RemoteException e) { 8437237839e87988208296a57d70767ebbd9c6880bJohn Grossman retVal = null; 8537237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 8637237839e87988208296a57d70767ebbd9c6880bJohn Grossman 8737237839e87988208296a57d70767ebbd9c6880bJohn Grossman return retVal; 8837237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 8937237839e87988208296a57d70767ebbd9c6880bJohn Grossman 9037237839e87988208296a57d70767ebbd9c6880bJohn Grossman /** 9137237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Release all native resources held by this {@link android.os.CommonTimeConfig} instance. Once 9237237839e87988208296a57d70767ebbd9c6880bJohn Grossman * resources have been released, the {@link android.os.CommonTimeConfig} instance is 9337237839e87988208296a57d70767ebbd9c6880bJohn Grossman * disconnected from the native service and will throw a {@link android.os.RemoteException} if 9437237839e87988208296a57d70767ebbd9c6880bJohn Grossman * any of its methods are called. Clients should always call release on their client instances 9537237839e87988208296a57d70767ebbd9c6880bJohn Grossman * before releasing their last Java reference to the instance. Failure to do this will cause 9637237839e87988208296a57d70767ebbd9c6880bJohn Grossman * non-deterministic native resource reclamation and may cause the common time service to remain 9737237839e87988208296a57d70767ebbd9c6880bJohn Grossman * active on the network for longer than it should. 9837237839e87988208296a57d70767ebbd9c6880bJohn Grossman */ 9937237839e87988208296a57d70767ebbd9c6880bJohn Grossman public void release() { 10037237839e87988208296a57d70767ebbd9c6880bJohn Grossman if (null != mRemote) { 10137237839e87988208296a57d70767ebbd9c6880bJohn Grossman try { 10237237839e87988208296a57d70767ebbd9c6880bJohn Grossman mRemote.unlinkToDeath(mDeathHandler, 0); 10337237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 10437237839e87988208296a57d70767ebbd9c6880bJohn Grossman catch (NoSuchElementException e) { } 10537237839e87988208296a57d70767ebbd9c6880bJohn Grossman mRemote = null; 10637237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 10737237839e87988208296a57d70767ebbd9c6880bJohn Grossman mUtils = null; 10837237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 10937237839e87988208296a57d70767ebbd9c6880bJohn Grossman 11037237839e87988208296a57d70767ebbd9c6880bJohn Grossman /** 11137237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Gets the current priority of the common time service used in the master election protocol. 11237237839e87988208296a57d70767ebbd9c6880bJohn Grossman * 11337237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @return an 8 bit value indicating the priority of this common time service relative to other 11437237839e87988208296a57d70767ebbd9c6880bJohn Grossman * common time services operating in the same domain. 11537237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @throws android.os.RemoteException 11637237839e87988208296a57d70767ebbd9c6880bJohn Grossman */ 11737237839e87988208296a57d70767ebbd9c6880bJohn Grossman public byte getMasterElectionPriority() 11837237839e87988208296a57d70767ebbd9c6880bJohn Grossman throws RemoteException { 11937237839e87988208296a57d70767ebbd9c6880bJohn Grossman throwOnDeadServer(); 12037237839e87988208296a57d70767ebbd9c6880bJohn Grossman return (byte)mUtils.transactGetInt(METHOD_GET_MASTER_ELECTION_PRIORITY, -1); 12137237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 12237237839e87988208296a57d70767ebbd9c6880bJohn Grossman 12337237839e87988208296a57d70767ebbd9c6880bJohn Grossman /** 12437237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Sets the current priority of the common time service used in the master election protocol. 12537237839e87988208296a57d70767ebbd9c6880bJohn Grossman * 12637237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @param priority priority of the common time service used in the master election protocol. 12737237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Lower numbers are lower priority. 12837237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @return {@link #SUCCESS} in case of success, 12937237839e87988208296a57d70767ebbd9c6880bJohn Grossman * {@link #ERROR} or {@link #ERROR_DEAD_OBJECT} in case of failure. 13037237839e87988208296a57d70767ebbd9c6880bJohn Grossman */ 13137237839e87988208296a57d70767ebbd9c6880bJohn Grossman public int setMasterElectionPriority(byte priority) { 13237237839e87988208296a57d70767ebbd9c6880bJohn Grossman if (checkDeadServer()) 13337237839e87988208296a57d70767ebbd9c6880bJohn Grossman return ERROR_DEAD_OBJECT; 13437237839e87988208296a57d70767ebbd9c6880bJohn Grossman return mUtils.transactSetInt(METHOD_SET_MASTER_ELECTION_PRIORITY, priority); 13537237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 13637237839e87988208296a57d70767ebbd9c6880bJohn Grossman 13737237839e87988208296a57d70767ebbd9c6880bJohn Grossman /** 13837237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Gets the IP endpoint used by the time service to participate in the master election protocol. 13937237839e87988208296a57d70767ebbd9c6880bJohn Grossman * 14037237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @return an InetSocketAddress containing the IP address and UDP port being used by the 14137237839e87988208296a57d70767ebbd9c6880bJohn Grossman * system's common time service to participate in the master election protocol. 14237237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @throws android.os.RemoteException 14337237839e87988208296a57d70767ebbd9c6880bJohn Grossman */ 14437237839e87988208296a57d70767ebbd9c6880bJohn Grossman public InetSocketAddress getMasterElectionEndpoint() 14537237839e87988208296a57d70767ebbd9c6880bJohn Grossman throws RemoteException { 14637237839e87988208296a57d70767ebbd9c6880bJohn Grossman throwOnDeadServer(); 14737237839e87988208296a57d70767ebbd9c6880bJohn Grossman return mUtils.transactGetSockaddr(METHOD_GET_MASTER_ELECTION_ENDPOINT); 14837237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 14937237839e87988208296a57d70767ebbd9c6880bJohn Grossman 15037237839e87988208296a57d70767ebbd9c6880bJohn Grossman /** 15137237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Sets the IP endpoint used by the common time service to participate in the master election 15237237839e87988208296a57d70767ebbd9c6880bJohn Grossman * protocol. 15337237839e87988208296a57d70767ebbd9c6880bJohn Grossman * 15437237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @param ep The IP address and UDP port to be used by the common time service to participate in 15537237839e87988208296a57d70767ebbd9c6880bJohn Grossman * the master election protocol. The supplied IP address must be either the broadcast or 15637237839e87988208296a57d70767ebbd9c6880bJohn Grossman * multicast address, unicast addresses are considered to be illegal values. 15737237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @return {@link #SUCCESS} in case of success, 15837237839e87988208296a57d70767ebbd9c6880bJohn Grossman * {@link #ERROR}, {@link #ERROR_BAD_VALUE} or {@link #ERROR_DEAD_OBJECT} in case of failure. 15937237839e87988208296a57d70767ebbd9c6880bJohn Grossman */ 16037237839e87988208296a57d70767ebbd9c6880bJohn Grossman public int setMasterElectionEndpoint(InetSocketAddress ep) { 16137237839e87988208296a57d70767ebbd9c6880bJohn Grossman if (checkDeadServer()) 16237237839e87988208296a57d70767ebbd9c6880bJohn Grossman return ERROR_DEAD_OBJECT; 16337237839e87988208296a57d70767ebbd9c6880bJohn Grossman return mUtils.transactSetSockaddr(METHOD_SET_MASTER_ELECTION_ENDPOINT, ep); 16437237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 16537237839e87988208296a57d70767ebbd9c6880bJohn Grossman 16637237839e87988208296a57d70767ebbd9c6880bJohn Grossman /** 16737237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Gets the current group ID used by the common time service in the master election protocol. 16837237839e87988208296a57d70767ebbd9c6880bJohn Grossman * 16937237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @return The 64-bit group ID of the common time service. 17037237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @throws android.os.RemoteException 17137237839e87988208296a57d70767ebbd9c6880bJohn Grossman */ 17237237839e87988208296a57d70767ebbd9c6880bJohn Grossman public long getMasterElectionGroupId() 17337237839e87988208296a57d70767ebbd9c6880bJohn Grossman throws RemoteException { 17437237839e87988208296a57d70767ebbd9c6880bJohn Grossman throwOnDeadServer(); 17537237839e87988208296a57d70767ebbd9c6880bJohn Grossman return mUtils.transactGetLong(METHOD_GET_MASTER_ELECTION_GROUP_ID, INVALID_GROUP_ID); 17637237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 17737237839e87988208296a57d70767ebbd9c6880bJohn Grossman 17837237839e87988208296a57d70767ebbd9c6880bJohn Grossman /** 17937237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Sets the current group ID used by the common time service in the master election protocol. 18037237839e87988208296a57d70767ebbd9c6880bJohn Grossman * 18137237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @param id The 64-bit group ID of the common time service. 18237237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @return {@link #SUCCESS} in case of success, 18337237839e87988208296a57d70767ebbd9c6880bJohn Grossman * {@link #ERROR}, {@link #ERROR_BAD_VALUE} or {@link #ERROR_DEAD_OBJECT} in case of failure. 18437237839e87988208296a57d70767ebbd9c6880bJohn Grossman */ 18537237839e87988208296a57d70767ebbd9c6880bJohn Grossman public int setMasterElectionGroupId(long id) { 18637237839e87988208296a57d70767ebbd9c6880bJohn Grossman if (checkDeadServer()) 18737237839e87988208296a57d70767ebbd9c6880bJohn Grossman return ERROR_DEAD_OBJECT; 18837237839e87988208296a57d70767ebbd9c6880bJohn Grossman return mUtils.transactSetLong(METHOD_SET_MASTER_ELECTION_GROUP_ID, id); 18937237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 19037237839e87988208296a57d70767ebbd9c6880bJohn Grossman 19137237839e87988208296a57d70767ebbd9c6880bJohn Grossman /** 19237237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Gets the name of the network interface which the common time service attempts to bind to. 19337237839e87988208296a57d70767ebbd9c6880bJohn Grossman * 19437237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @return a string with the network interface name which the common time service is bound to, 19537237839e87988208296a57d70767ebbd9c6880bJohn Grossman * or null if the service is currently unbound. Examples of interface names are things like 19637237839e87988208296a57d70767ebbd9c6880bJohn Grossman * "eth0", or "wlan0". 19737237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @throws android.os.RemoteException 19837237839e87988208296a57d70767ebbd9c6880bJohn Grossman */ 19937237839e87988208296a57d70767ebbd9c6880bJohn Grossman public String getInterfaceBinding() 20037237839e87988208296a57d70767ebbd9c6880bJohn Grossman throws RemoteException { 20137237839e87988208296a57d70767ebbd9c6880bJohn Grossman throwOnDeadServer(); 20237237839e87988208296a57d70767ebbd9c6880bJohn Grossman 20337237839e87988208296a57d70767ebbd9c6880bJohn Grossman String ifaceName = mUtils.transactGetString(METHOD_GET_INTERFACE_BINDING, null); 20437237839e87988208296a57d70767ebbd9c6880bJohn Grossman 20537237839e87988208296a57d70767ebbd9c6880bJohn Grossman if ((null != ifaceName) && (0 == ifaceName.length())) 20637237839e87988208296a57d70767ebbd9c6880bJohn Grossman return null; 20737237839e87988208296a57d70767ebbd9c6880bJohn Grossman 20837237839e87988208296a57d70767ebbd9c6880bJohn Grossman return ifaceName; 20937237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 21037237839e87988208296a57d70767ebbd9c6880bJohn Grossman 21137237839e87988208296a57d70767ebbd9c6880bJohn Grossman /** 21237237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Sets the name of the network interface which the common time service should attempt to bind 21337237839e87988208296a57d70767ebbd9c6880bJohn Grossman * to. 21437237839e87988208296a57d70767ebbd9c6880bJohn Grossman * 21537237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @param ifaceName The name of the network interface ("eth0", "wlan0", etc...) wich the common 21637237839e87988208296a57d70767ebbd9c6880bJohn Grossman * time service should attempt to bind to, or null to force the common time service to unbind 21737237839e87988208296a57d70767ebbd9c6880bJohn Grossman * from the network and run in networkless mode. 21837237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @return {@link #SUCCESS} in case of success, 21937237839e87988208296a57d70767ebbd9c6880bJohn Grossman * {@link #ERROR}, {@link #ERROR_BAD_VALUE} or {@link #ERROR_DEAD_OBJECT} in case of failure. 22037237839e87988208296a57d70767ebbd9c6880bJohn Grossman */ 22137237839e87988208296a57d70767ebbd9c6880bJohn Grossman public int setNetworkBinding(String ifaceName) { 22237237839e87988208296a57d70767ebbd9c6880bJohn Grossman if (checkDeadServer()) 22337237839e87988208296a57d70767ebbd9c6880bJohn Grossman return ERROR_DEAD_OBJECT; 22437237839e87988208296a57d70767ebbd9c6880bJohn Grossman 22537237839e87988208296a57d70767ebbd9c6880bJohn Grossman return mUtils.transactSetString(METHOD_SET_INTERFACE_BINDING, 22637237839e87988208296a57d70767ebbd9c6880bJohn Grossman (null == ifaceName) ? "" : ifaceName); 22737237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 22837237839e87988208296a57d70767ebbd9c6880bJohn Grossman 22937237839e87988208296a57d70767ebbd9c6880bJohn Grossman /** 23037237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Gets the amount of time the common time service will wait between master announcements when 23137237839e87988208296a57d70767ebbd9c6880bJohn Grossman * it is the timeline master. 23237237839e87988208296a57d70767ebbd9c6880bJohn Grossman * 23337237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @return The time (in milliseconds) between master announcements. 23437237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @throws android.os.RemoteException 23537237839e87988208296a57d70767ebbd9c6880bJohn Grossman */ 23637237839e87988208296a57d70767ebbd9c6880bJohn Grossman public int getMasterAnnounceInterval() 23737237839e87988208296a57d70767ebbd9c6880bJohn Grossman throws RemoteException { 23837237839e87988208296a57d70767ebbd9c6880bJohn Grossman throwOnDeadServer(); 23937237839e87988208296a57d70767ebbd9c6880bJohn Grossman return mUtils.transactGetInt(METHOD_GET_MASTER_ANNOUNCE_INTERVAL, -1); 24037237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 24137237839e87988208296a57d70767ebbd9c6880bJohn Grossman 24237237839e87988208296a57d70767ebbd9c6880bJohn Grossman /** 24337237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Sets the amount of time the common time service will wait between master announcements when 24437237839e87988208296a57d70767ebbd9c6880bJohn Grossman * it is the timeline master. 24537237839e87988208296a57d70767ebbd9c6880bJohn Grossman * 24637237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @param interval The time (in milliseconds) between master announcements. 24737237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @return {@link #SUCCESS} in case of success, 24837237839e87988208296a57d70767ebbd9c6880bJohn Grossman * {@link #ERROR}, {@link #ERROR_BAD_VALUE} or {@link #ERROR_DEAD_OBJECT} in case of failure. 24937237839e87988208296a57d70767ebbd9c6880bJohn Grossman */ 25037237839e87988208296a57d70767ebbd9c6880bJohn Grossman public int setMasterAnnounceInterval(int interval) { 25137237839e87988208296a57d70767ebbd9c6880bJohn Grossman if (checkDeadServer()) 25237237839e87988208296a57d70767ebbd9c6880bJohn Grossman return ERROR_DEAD_OBJECT; 25337237839e87988208296a57d70767ebbd9c6880bJohn Grossman return mUtils.transactSetInt(METHOD_SET_MASTER_ANNOUNCE_INTERVAL, interval); 25437237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 25537237839e87988208296a57d70767ebbd9c6880bJohn Grossman 25637237839e87988208296a57d70767ebbd9c6880bJohn Grossman /** 25737237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Gets the amount of time the common time service will wait between time synchronization 25837237839e87988208296a57d70767ebbd9c6880bJohn Grossman * requests when it is the client of another common time service on the network. 25937237839e87988208296a57d70767ebbd9c6880bJohn Grossman * 26037237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @return The time (in milliseconds) between time sync requests. 26137237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @throws android.os.RemoteException 26237237839e87988208296a57d70767ebbd9c6880bJohn Grossman */ 26337237839e87988208296a57d70767ebbd9c6880bJohn Grossman public int getClientSyncInterval() 26437237839e87988208296a57d70767ebbd9c6880bJohn Grossman throws RemoteException { 26537237839e87988208296a57d70767ebbd9c6880bJohn Grossman throwOnDeadServer(); 26637237839e87988208296a57d70767ebbd9c6880bJohn Grossman return mUtils.transactGetInt(METHOD_GET_CLIENT_SYNC_INTERVAL, -1); 26737237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 26837237839e87988208296a57d70767ebbd9c6880bJohn Grossman 26937237839e87988208296a57d70767ebbd9c6880bJohn Grossman /** 27037237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Sets the amount of time the common time service will wait between time synchronization 27137237839e87988208296a57d70767ebbd9c6880bJohn Grossman * requests when it is the client of another common time service on the network. 27237237839e87988208296a57d70767ebbd9c6880bJohn Grossman * 27337237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @param interval The time (in milliseconds) between time sync requests. 27437237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @return {@link #SUCCESS} in case of success, 27537237839e87988208296a57d70767ebbd9c6880bJohn Grossman * {@link #ERROR}, {@link #ERROR_BAD_VALUE} or {@link #ERROR_DEAD_OBJECT} in case of failure. 27637237839e87988208296a57d70767ebbd9c6880bJohn Grossman */ 27737237839e87988208296a57d70767ebbd9c6880bJohn Grossman public int setClientSyncInterval(int interval) { 27837237839e87988208296a57d70767ebbd9c6880bJohn Grossman if (checkDeadServer()) 27937237839e87988208296a57d70767ebbd9c6880bJohn Grossman return ERROR_DEAD_OBJECT; 28037237839e87988208296a57d70767ebbd9c6880bJohn Grossman return mUtils.transactSetInt(METHOD_SET_CLIENT_SYNC_INTERVAL, interval); 28137237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 28237237839e87988208296a57d70767ebbd9c6880bJohn Grossman 28337237839e87988208296a57d70767ebbd9c6880bJohn Grossman /** 28437237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Gets the panic threshold for the estimated error level of the common time service. When the 28537237839e87988208296a57d70767ebbd9c6880bJohn Grossman * common time service's estimated error rises above this level, the service will panic and 28637237839e87988208296a57d70767ebbd9c6880bJohn Grossman * reset, causing a discontinuity in the currently synchronized timeline. 28737237839e87988208296a57d70767ebbd9c6880bJohn Grossman * 28837237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @return The threshold (in microseconds) past which the common time service will panic. 28937237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @throws android.os.RemoteException 29037237839e87988208296a57d70767ebbd9c6880bJohn Grossman */ 29137237839e87988208296a57d70767ebbd9c6880bJohn Grossman public int getPanicThreshold() 29237237839e87988208296a57d70767ebbd9c6880bJohn Grossman throws RemoteException { 29337237839e87988208296a57d70767ebbd9c6880bJohn Grossman throwOnDeadServer(); 29437237839e87988208296a57d70767ebbd9c6880bJohn Grossman return mUtils.transactGetInt(METHOD_GET_PANIC_THRESHOLD, -1); 29537237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 29637237839e87988208296a57d70767ebbd9c6880bJohn Grossman 29737237839e87988208296a57d70767ebbd9c6880bJohn Grossman /** 29837237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Sets the panic threshold for the estimated error level of the common time service. When the 29937237839e87988208296a57d70767ebbd9c6880bJohn Grossman * common time service's estimated error rises above this level, the service will panic and 30037237839e87988208296a57d70767ebbd9c6880bJohn Grossman * reset, causing a discontinuity in the currently synchronized timeline. 30137237839e87988208296a57d70767ebbd9c6880bJohn Grossman * 30237237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @param threshold The threshold (in microseconds) past which the common time service will 30337237839e87988208296a57d70767ebbd9c6880bJohn Grossman * panic. 30437237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @return {@link #SUCCESS} in case of success, 30537237839e87988208296a57d70767ebbd9c6880bJohn Grossman * {@link #ERROR}, {@link #ERROR_BAD_VALUE} or {@link #ERROR_DEAD_OBJECT} in case of failure. 30637237839e87988208296a57d70767ebbd9c6880bJohn Grossman */ 30737237839e87988208296a57d70767ebbd9c6880bJohn Grossman public int setPanicThreshold(int threshold) { 30837237839e87988208296a57d70767ebbd9c6880bJohn Grossman if (checkDeadServer()) 30937237839e87988208296a57d70767ebbd9c6880bJohn Grossman return ERROR_DEAD_OBJECT; 31037237839e87988208296a57d70767ebbd9c6880bJohn Grossman return mUtils.transactSetInt(METHOD_SET_PANIC_THRESHOLD, threshold); 31137237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 31237237839e87988208296a57d70767ebbd9c6880bJohn Grossman 31337237839e87988208296a57d70767ebbd9c6880bJohn Grossman /** 31437237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Gets the current state of the common time service's auto disable flag. 31537237839e87988208296a57d70767ebbd9c6880bJohn Grossman * 31637237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @return The current state of the common time service's auto disable flag. 31737237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @throws android.os.RemoteException 31837237839e87988208296a57d70767ebbd9c6880bJohn Grossman */ 31937237839e87988208296a57d70767ebbd9c6880bJohn Grossman public boolean getAutoDisable() 32037237839e87988208296a57d70767ebbd9c6880bJohn Grossman throws RemoteException { 32137237839e87988208296a57d70767ebbd9c6880bJohn Grossman throwOnDeadServer(); 32237237839e87988208296a57d70767ebbd9c6880bJohn Grossman return (1 == mUtils.transactGetInt(METHOD_GET_AUTO_DISABLE, 1)); 32337237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 32437237839e87988208296a57d70767ebbd9c6880bJohn Grossman 32537237839e87988208296a57d70767ebbd9c6880bJohn Grossman /** 32637237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Sets the current state of the common time service's auto disable flag. When the time 32737237839e87988208296a57d70767ebbd9c6880bJohn Grossman * service's auto disable flag is set, it will automatically cease all network activity when 32837237839e87988208296a57d70767ebbd9c6880bJohn Grossman * it has no active local clients, resuming activity the next time the service has interested 32937237839e87988208296a57d70767ebbd9c6880bJohn Grossman * local clients. When the auto disabled flag is cleared, the common time service will continue 33037237839e87988208296a57d70767ebbd9c6880bJohn Grossman * to participate the time synchronization group even when it has no active local clients. 33137237839e87988208296a57d70767ebbd9c6880bJohn Grossman * 33237237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @param autoDisable The desired state of the common time service's auto disable flag. 33337237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @return {@link #SUCCESS} in case of success, 33437237839e87988208296a57d70767ebbd9c6880bJohn Grossman * {@link #ERROR} or {@link #ERROR_DEAD_OBJECT} in case of failure. 33537237839e87988208296a57d70767ebbd9c6880bJohn Grossman */ 33637237839e87988208296a57d70767ebbd9c6880bJohn Grossman public int setAutoDisable(boolean autoDisable) { 33737237839e87988208296a57d70767ebbd9c6880bJohn Grossman if (checkDeadServer()) 33837237839e87988208296a57d70767ebbd9c6880bJohn Grossman return ERROR_DEAD_OBJECT; 33937237839e87988208296a57d70767ebbd9c6880bJohn Grossman 34037237839e87988208296a57d70767ebbd9c6880bJohn Grossman return mUtils.transactSetInt(METHOD_SET_AUTO_DISABLE, autoDisable ? 1 : 0); 34137237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 34237237839e87988208296a57d70767ebbd9c6880bJohn Grossman 34337237839e87988208296a57d70767ebbd9c6880bJohn Grossman /** 34437237839e87988208296a57d70767ebbd9c6880bJohn Grossman * At startup, the time service enters the initial state and remains there until it is given a 34537237839e87988208296a57d70767ebbd9c6880bJohn Grossman * network interface to bind to. Common time will be unavailable to clients of the common time 34637237839e87988208296a57d70767ebbd9c6880bJohn Grossman * service until the service joins a network (even an empty network). Devices may use the 34737237839e87988208296a57d70767ebbd9c6880bJohn Grossman * {@link #forceNetworklessMasterMode()} method to force a time service in the INITIAL state 34837237839e87988208296a57d70767ebbd9c6880bJohn Grossman * with no network configuration to assume MASTER status for a brand new timeline in order to 34937237839e87988208296a57d70767ebbd9c6880bJohn Grossman * allow clients of the common time service to operate, even though the device is isolated and 35037237839e87988208296a57d70767ebbd9c6880bJohn Grossman * not on any network. When a networkless master does join a network, it will defer to any 35137237839e87988208296a57d70767ebbd9c6880bJohn Grossman * masters already on the network, or continue to maintain the timeline it made up during its 35237237839e87988208296a57d70767ebbd9c6880bJohn Grossman * networkless state if no other masters are detected. Attempting to force a client into master 35337237839e87988208296a57d70767ebbd9c6880bJohn Grossman * mode while it is actively bound to a network will fail with the status code {@link #ERROR} 35437237839e87988208296a57d70767ebbd9c6880bJohn Grossman * 35537237839e87988208296a57d70767ebbd9c6880bJohn Grossman * @return {@link #SUCCESS} in case of success, 35637237839e87988208296a57d70767ebbd9c6880bJohn Grossman * {@link #ERROR} or {@link #ERROR_DEAD_OBJECT} in case of failure. 35737237839e87988208296a57d70767ebbd9c6880bJohn Grossman */ 35837237839e87988208296a57d70767ebbd9c6880bJohn Grossman public int forceNetworklessMasterMode() { 35937237839e87988208296a57d70767ebbd9c6880bJohn Grossman android.os.Parcel data = android.os.Parcel.obtain(); 36037237839e87988208296a57d70767ebbd9c6880bJohn Grossman android.os.Parcel reply = android.os.Parcel.obtain(); 36137237839e87988208296a57d70767ebbd9c6880bJohn Grossman 36237237839e87988208296a57d70767ebbd9c6880bJohn Grossman try { 36337237839e87988208296a57d70767ebbd9c6880bJohn Grossman data.writeInterfaceToken(mInterfaceDesc); 36437237839e87988208296a57d70767ebbd9c6880bJohn Grossman mRemote.transact(METHOD_FORCE_NETWORKLESS_MASTER_MODE, data, reply, 0); 36537237839e87988208296a57d70767ebbd9c6880bJohn Grossman 36637237839e87988208296a57d70767ebbd9c6880bJohn Grossman return reply.readInt(); 36737237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 36837237839e87988208296a57d70767ebbd9c6880bJohn Grossman catch (RemoteException e) { 36937237839e87988208296a57d70767ebbd9c6880bJohn Grossman return ERROR_DEAD_OBJECT; 37037237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 37137237839e87988208296a57d70767ebbd9c6880bJohn Grossman finally { 37237237839e87988208296a57d70767ebbd9c6880bJohn Grossman reply.recycle(); 37337237839e87988208296a57d70767ebbd9c6880bJohn Grossman data.recycle(); 37437237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 37537237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 37637237839e87988208296a57d70767ebbd9c6880bJohn Grossman 37737237839e87988208296a57d70767ebbd9c6880bJohn Grossman /** 37837237839e87988208296a57d70767ebbd9c6880bJohn Grossman * The OnServerDiedListener interface defines a method called by the 37937237839e87988208296a57d70767ebbd9c6880bJohn Grossman * {@link android.os.CommonTimeConfig} instance to indicate that the connection to the native 38037237839e87988208296a57d70767ebbd9c6880bJohn Grossman * media server has been broken and that the {@link android.os.CommonTimeConfig} instance will 38137237839e87988208296a57d70767ebbd9c6880bJohn Grossman * need to be released and re-created. The client application can implement this interface and 38237237839e87988208296a57d70767ebbd9c6880bJohn Grossman * register the listener with the {@link #setServerDiedListener(OnServerDiedListener)} method. 38337237839e87988208296a57d70767ebbd9c6880bJohn Grossman */ 38437237839e87988208296a57d70767ebbd9c6880bJohn Grossman public interface OnServerDiedListener { 38537237839e87988208296a57d70767ebbd9c6880bJohn Grossman /** 38637237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Method called when the native common time service has died. <p>If the native common time 38737237839e87988208296a57d70767ebbd9c6880bJohn Grossman * service encounters a fatal error and needs to restart, the binder connection from the 38837237839e87988208296a57d70767ebbd9c6880bJohn Grossman * {@link android.os.CommonTimeConfig} instance to the common time service will be broken. 38937237839e87988208296a57d70767ebbd9c6880bJohn Grossman */ 39037237839e87988208296a57d70767ebbd9c6880bJohn Grossman void onServerDied(); 39137237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 39237237839e87988208296a57d70767ebbd9c6880bJohn Grossman 39337237839e87988208296a57d70767ebbd9c6880bJohn Grossman /** 39437237839e87988208296a57d70767ebbd9c6880bJohn Grossman * Registers an OnServerDiedListener interface. 39537237839e87988208296a57d70767ebbd9c6880bJohn Grossman * <p>Call this method with a null listener to stop receiving server death notifications. 39637237839e87988208296a57d70767ebbd9c6880bJohn Grossman */ 39737237839e87988208296a57d70767ebbd9c6880bJohn Grossman public void setServerDiedListener(OnServerDiedListener listener) { 39837237839e87988208296a57d70767ebbd9c6880bJohn Grossman synchronized (mListenerLock) { 39937237839e87988208296a57d70767ebbd9c6880bJohn Grossman mServerDiedListener = listener; 40037237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 40137237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 40237237839e87988208296a57d70767ebbd9c6880bJohn Grossman 40337237839e87988208296a57d70767ebbd9c6880bJohn Grossman protected void finalize() throws Throwable { release(); } 40437237839e87988208296a57d70767ebbd9c6880bJohn Grossman 40537237839e87988208296a57d70767ebbd9c6880bJohn Grossman private boolean checkDeadServer() { 40637237839e87988208296a57d70767ebbd9c6880bJohn Grossman return ((null == mRemote) || (null == mUtils)); 40737237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 40837237839e87988208296a57d70767ebbd9c6880bJohn Grossman 40937237839e87988208296a57d70767ebbd9c6880bJohn Grossman private void throwOnDeadServer() throws RemoteException { 41037237839e87988208296a57d70767ebbd9c6880bJohn Grossman if (checkDeadServer()) 41137237839e87988208296a57d70767ebbd9c6880bJohn Grossman throw new RemoteException(); 41237237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 41337237839e87988208296a57d70767ebbd9c6880bJohn Grossman 41437237839e87988208296a57d70767ebbd9c6880bJohn Grossman private final Object mListenerLock = new Object(); 41537237839e87988208296a57d70767ebbd9c6880bJohn Grossman private OnServerDiedListener mServerDiedListener = null; 41637237839e87988208296a57d70767ebbd9c6880bJohn Grossman 41737237839e87988208296a57d70767ebbd9c6880bJohn Grossman private IBinder mRemote = null; 41837237839e87988208296a57d70767ebbd9c6880bJohn Grossman private String mInterfaceDesc = ""; 41937237839e87988208296a57d70767ebbd9c6880bJohn Grossman private CommonTimeUtils mUtils; 42037237839e87988208296a57d70767ebbd9c6880bJohn Grossman 42137237839e87988208296a57d70767ebbd9c6880bJohn Grossman private IBinder.DeathRecipient mDeathHandler = new IBinder.DeathRecipient() { 42237237839e87988208296a57d70767ebbd9c6880bJohn Grossman public void binderDied() { 42337237839e87988208296a57d70767ebbd9c6880bJohn Grossman synchronized (mListenerLock) { 42437237839e87988208296a57d70767ebbd9c6880bJohn Grossman if (null != mServerDiedListener) 42537237839e87988208296a57d70767ebbd9c6880bJohn Grossman mServerDiedListener.onServerDied(); 42637237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 42737237839e87988208296a57d70767ebbd9c6880bJohn Grossman } 42837237839e87988208296a57d70767ebbd9c6880bJohn Grossman }; 42937237839e87988208296a57d70767ebbd9c6880bJohn Grossman 43037237839e87988208296a57d70767ebbd9c6880bJohn Grossman private static final int METHOD_GET_MASTER_ELECTION_PRIORITY = IBinder.FIRST_CALL_TRANSACTION; 43137237839e87988208296a57d70767ebbd9c6880bJohn Grossman private static final int METHOD_SET_MASTER_ELECTION_PRIORITY = METHOD_GET_MASTER_ELECTION_PRIORITY + 1; 43237237839e87988208296a57d70767ebbd9c6880bJohn Grossman private static final int METHOD_GET_MASTER_ELECTION_ENDPOINT = METHOD_SET_MASTER_ELECTION_PRIORITY + 1; 43337237839e87988208296a57d70767ebbd9c6880bJohn Grossman private static final int METHOD_SET_MASTER_ELECTION_ENDPOINT = METHOD_GET_MASTER_ELECTION_ENDPOINT + 1; 43437237839e87988208296a57d70767ebbd9c6880bJohn Grossman private static final int METHOD_GET_MASTER_ELECTION_GROUP_ID = METHOD_SET_MASTER_ELECTION_ENDPOINT + 1; 43537237839e87988208296a57d70767ebbd9c6880bJohn Grossman private static final int METHOD_SET_MASTER_ELECTION_GROUP_ID = METHOD_GET_MASTER_ELECTION_GROUP_ID + 1; 43637237839e87988208296a57d70767ebbd9c6880bJohn Grossman private static final int METHOD_GET_INTERFACE_BINDING = METHOD_SET_MASTER_ELECTION_GROUP_ID + 1; 43737237839e87988208296a57d70767ebbd9c6880bJohn Grossman private static final int METHOD_SET_INTERFACE_BINDING = METHOD_GET_INTERFACE_BINDING + 1; 43837237839e87988208296a57d70767ebbd9c6880bJohn Grossman private static final int METHOD_GET_MASTER_ANNOUNCE_INTERVAL = METHOD_SET_INTERFACE_BINDING + 1; 43937237839e87988208296a57d70767ebbd9c6880bJohn Grossman private static final int METHOD_SET_MASTER_ANNOUNCE_INTERVAL = METHOD_GET_MASTER_ANNOUNCE_INTERVAL + 1; 44037237839e87988208296a57d70767ebbd9c6880bJohn Grossman private static final int METHOD_GET_CLIENT_SYNC_INTERVAL = METHOD_SET_MASTER_ANNOUNCE_INTERVAL + 1; 44137237839e87988208296a57d70767ebbd9c6880bJohn Grossman private static final int METHOD_SET_CLIENT_SYNC_INTERVAL = METHOD_GET_CLIENT_SYNC_INTERVAL + 1; 44237237839e87988208296a57d70767ebbd9c6880bJohn Grossman private static final int METHOD_GET_PANIC_THRESHOLD = METHOD_SET_CLIENT_SYNC_INTERVAL + 1; 44337237839e87988208296a57d70767ebbd9c6880bJohn Grossman private static final int METHOD_SET_PANIC_THRESHOLD = METHOD_GET_PANIC_THRESHOLD + 1; 44437237839e87988208296a57d70767ebbd9c6880bJohn Grossman private static final int METHOD_GET_AUTO_DISABLE = METHOD_SET_PANIC_THRESHOLD + 1; 44537237839e87988208296a57d70767ebbd9c6880bJohn Grossman private static final int METHOD_SET_AUTO_DISABLE = METHOD_GET_AUTO_DISABLE + 1; 44637237839e87988208296a57d70767ebbd9c6880bJohn Grossman private static final int METHOD_FORCE_NETWORKLESS_MASTER_MODE = METHOD_SET_AUTO_DISABLE + 1; 44737237839e87988208296a57d70767ebbd9c6880bJohn Grossman} 448